joinbook.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. let util = require('../../../utils/util.js');
  2. var that
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. uss: []
  9. },
  10. onGotUserInfo(e) {
  11. this.bindphone(e.detail.userInfo)
  12. },
  13. bindphone(ui) {
  14. wx.request({
  15. url: util.baseurl + 'cuser/wxapi/binduserinfo',
  16. data: {
  17. phone: '',
  18. avatarUrl: ui.avatarUrl,
  19. nickName: ui.nickName
  20. },
  21. dataType: 'json',
  22. success: function(d) {
  23. wx.hideLoading();
  24. if (d.statusCode === 200) {
  25. that.setData({
  26. user: d.data.userInfo
  27. })
  28. that.joinbook()
  29. } else {
  30. wx.showToast({
  31. title: '请求失败',
  32. icon: 'none',
  33. duration: 2000
  34. })
  35. }
  36. },
  37. method: 'POST',
  38. header: {
  39. 'content-type': 'application/x-www-form-urlencoded',
  40. 'token': this.data.token
  41. }
  42. })
  43. },
  44. findUser(id) {
  45. util.query('userinfo/wxapi/findbyid', {
  46. id: id
  47. }, function(res) {
  48. if (res.code === 10000) {
  49. that.setData({
  50. u: res.data
  51. })
  52. } else {
  53. }
  54. })
  55. },
  56. join() {
  57. if (!this.data.user) {
  58. util.showToast('还未登录,请先登录')
  59. setTimeout()
  60. } else {
  61. this.joinbook()
  62. }
  63. },
  64. joinbook() {
  65. util.query('accountbook/wxapi/join', {
  66. accountBookId: this.data.nowbook.id,
  67. userIds: [{
  68. userId: this.data.user.id
  69. }]
  70. }, function(res) {
  71. if (res.code === 10000) {
  72. util.showToast('加入成功')
  73. that.getDefaultBook()
  74. } else {
  75. util.showToast('加入失败')
  76. }
  77. }, 'post')
  78. },
  79. checkjoin() {
  80. var myid = this.data.user.id
  81. this.data.nowbook.userInfos.forEach(function(res) {
  82. if (res.id === myid) {
  83. that.setData({
  84. isjoin: true
  85. })
  86. return
  87. }
  88. })
  89. },
  90. wxlogin() {
  91. wx.login({
  92. success: function(res) {
  93. if (res.code) {
  94. util.query(
  95. "/cuser/wxapi/getopenid", {
  96. code: res.code
  97. },
  98. function(da) {
  99. if (da.code === 10000) {
  100. wx.setStorageSync("token", da.token)
  101. that.setData({
  102. token: da.token
  103. })
  104. // wx.setStorageSync("userinfo", da.userinfo)
  105. that.getDefaultBook()
  106. that.findUser(that.data.uid)
  107. if (da.registed) {
  108. that.setData({
  109. user: da.userinfo,
  110. registed: true
  111. })
  112. } else {
  113. that.setData({
  114. registed: false
  115. })
  116. }
  117. } else {
  118. wx.showToast({
  119. title: '登录失败',
  120. icon: 'none',
  121. duration: 2000
  122. })
  123. }
  124. }, "get", 1
  125. )
  126. } else {
  127. wx.showToast({
  128. title: '登录失败',
  129. icon: 'none',
  130. duration: 2000
  131. })
  132. console.log('获取用户登录态失败!' + res.errMsg)
  133. }
  134. }
  135. })
  136. },
  137. /**
  138. * 生命周期函数--监听页面加载
  139. */
  140. onLoad: function(options) {
  141. that = this
  142. this.setData({
  143. id: options.id,
  144. uid: options.uid
  145. })
  146. that.wxlogin()
  147. },
  148. getDefaultBook() {
  149. util.query('accountbook/wxapi/findbyid', {
  150. id: this.data.id
  151. }, function(res) {
  152. if (res.code === 10000) {
  153. var uss = []
  154. var us = res.data.userInfos
  155. for (var i = 0; i < parseInt(us.length / 5) + 1; i++) {
  156. uss[i] = []
  157. for (var j = 5 * i; j < (i + 1) * 5; j++) {
  158. if (us[j]) {
  159. uss[i].push({
  160. name: us[j].wechatName,
  161. logo: us[j].wechatIcon
  162. })
  163. } else {
  164. uss[i].push({
  165. no: 1
  166. })
  167. }
  168. }
  169. }
  170. that.setData({
  171. nowbook: res.data,
  172. uss: uss
  173. })
  174. that.checkjoin()
  175. }
  176. })
  177. },
  178. /**
  179. * 生命周期函数--监听页面初次渲染完成
  180. */
  181. onReady: function() {
  182. },
  183. /**
  184. * 生命周期函数--监听页面显示
  185. */
  186. onShow: function() {
  187. },
  188. /**
  189. * 生命周期函数--监听页面隐藏
  190. */
  191. onHide: function() {
  192. },
  193. /**
  194. * 生命周期函数--监听页面卸载
  195. */
  196. onUnload: function() {
  197. },
  198. /**
  199. * 页面相关事件处理函数--监听用户下拉动作
  200. */
  201. onPullDownRefresh: function() {
  202. },
  203. /**
  204. * 页面上拉触底事件的处理函数
  205. */
  206. onReachBottom: function() {
  207. },
  208. /**
  209. * 用户点击右上角分享
  210. */
  211. onShareAppMessage: function() {
  212. }
  213. })