user.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // pages/user/user.js
  2. const app = getApp()
  3. var host = app.globalData.host;
  4. const $api = require('../../utils/api.js').API;
  5. var logindata={}
  6. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. usercode: '',
  13. phone:'',
  14. phcode:'',
  15. openid: '',
  16. info: {},
  17. userinfo:{},
  18. todayinfo:{},
  19. todayMoney:'',
  20. status:0,
  21. text:'获取验证码',
  22. player_type:0,
  23. signinfo:{},
  24. avatarUrl: defaultAvatarUrl,
  25. userName:'',
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad: function (options) {
  31. if(options.type){
  32. this.setData({
  33. status:options.type,
  34. player_type:options.player_type
  35. })
  36. }
  37. $api.getsignup().then(res=>{
  38. this.setData({
  39. signinfo:res.data.data
  40. })
  41. })
  42. },
  43. onShow: function () {
  44. wx.hideHomeButton();
  45. },
  46. zx(){
  47. wx.navigateTo({
  48. url: '../message/message',
  49. })
  50. },
  51. getcode(){
  52. if(this.data.phone.length>=11){
  53. $api.sendcode({phone:this.data.phone}).then(res=>{
  54. this.countdown()
  55. })
  56. }else{
  57. wx.showToast({
  58. icon: 'none',
  59. title: '请输入正确的手机号',
  60. })
  61. }
  62. },
  63. countdown(){
  64. var count=60,_this=this;
  65. var timer=setInterval(c,1000)
  66. function c(){
  67. if(count>0){
  68. count--;
  69. _this.setData({
  70. text:count+"s重新发送"
  71. })
  72. }else{
  73. _this.setData({
  74. text:'获取验证码'
  75. })
  76. clearInterval(timer)
  77. }
  78. }
  79. },
  80. onChooseAvatar(e) {
  81. const { avatarUrl } = e.detail
  82. this.setData({
  83. avatarUrl,
  84. })
  85. this.uploadImg(this.data.avatarUrl)
  86. },
  87. nameChange(e){
  88. this.setData({
  89. userName: e.detail.value
  90. })
  91. },
  92. inputchange(e) {
  93. this.setData({
  94. phone: e.detail.value
  95. })
  96. },
  97. codechange(e) {
  98. this.setData({
  99. phcode: e.detail.value
  100. })
  101. },
  102. logout() {
  103. wx.removeStorage({
  104. key: 'userInfo'
  105. })
  106. wx.setNavigationBarTitle({
  107. title: '登录',
  108. })
  109. this.setData({
  110. userinfo: {},
  111. usercode: ''
  112. })
  113. },
  114. /**登陆 */
  115. login: function () {
  116. var _this=this;
  117. wx.getUserProfile({
  118. desc: '用于完善会员资料',
  119. success: res=> {
  120. //获取基本信息
  121. console.log(res.userInfo)
  122. var data = {
  123. nickName: res.userInfo.nickName,
  124. avatarUrl: res.userInfo.avatarUrl,
  125. usercode: this.data.usercode
  126. }
  127. if(data.nickName == "微信用户" | data.nickName == ""){
  128. _this.setData({
  129. status:10,
  130. })
  131. } else{
  132. _this.wxlogin(data)
  133. }
  134. }
  135. })
  136. },
  137. register(e){
  138. var data = {
  139. nickName: this.data.userName,
  140. avatarUrl: this.data.avatarUrl,
  141. usercode: this.data.usercode
  142. }
  143. this.wxlogin(data)
  144. },
  145. wxlogin(data){
  146. var _this=this;
  147. wx.showLoading({
  148. title: '正在登陆',
  149. })
  150. wx.login({
  151. success(res) {
  152. //获取code
  153. $api.getOpenid({ code: res.code})
  154. .then(res => {
  155. //获取openid
  156. if (res.data.code != 0) {
  157. wx.showToast({
  158. icon: 'none',
  159. title: res.data.message,
  160. })
  161. wx.hideLoading();
  162. return;
  163. }
  164. data.openid = res.data.data.openid;
  165. $api.login(data)
  166. .then(res=>{
  167. //登录成功
  168. wx.setStorage({
  169. key: 'userInfo',
  170. data: res.data.data,
  171. })
  172. //老用户直接进入系统
  173. if(res.data.data.role != 0){
  174. wx.switchTab({
  175. url: '../index/index',
  176. })
  177. return;
  178. }
  179. //新用户选择报名
  180. _this.setData({
  181. status:3,
  182. player_type:0
  183. })
  184. wx.hideLoading();
  185. wx.setNavigationBarTitle({
  186. title: '',
  187. })
  188. if (res.data.code != 0) {
  189. wx.showToast({
  190. icon: 'none',
  191. title: res.data.message,
  192. })
  193. return;
  194. }
  195. })
  196. .catch(err=>{
  197. wx.hideLoading();
  198. })
  199. })
  200. .catch(err => {
  201. //请求失败
  202. wx.hideLoading();
  203. })
  204. }
  205. })
  206. },
  207. login1(){
  208. logindata.phone=this.data.phone
  209. logindata.phcode=this.data.phcode
  210. if (logindata.phone.length<11){
  211. wx.showToast({
  212. icon: 'none',
  213. title: '请输入正确的手机号',
  214. })
  215. return
  216. }
  217. if (!logindata.phcode) {
  218. wx.showToast({
  219. icon: 'none',
  220. title: '请输入验证码',
  221. })
  222. return
  223. }
  224. $api.login(logindata).then(res=>{
  225. if(res.data.code == 0){
  226. this.setData({
  227. status:3
  228. })
  229. }
  230. })
  231. },
  232. login2(){
  233. if (this.data.phone.length<11) {
  234. wx.showToast({
  235. icon: 'none',
  236. title: '请输入正确的手机号',
  237. })
  238. return
  239. }
  240. if (!this.data.phcode) {
  241. wx.showToast({
  242. icon: 'none',
  243. title: '请输入验证码',
  244. })
  245. return
  246. }
  247. $api.bindphone({phone:this.data.phone,phcode:this.data.phcode}).then(res=>{
  248. if(res.data.code == 0){
  249. this.setData({
  250. status:3
  251. })
  252. }
  253. })
  254. },
  255. uploadImg(path){
  256. $api.uploadAvatar(path,'file').then(res=>{
  257. if(res.code == 0){
  258. this.setData({
  259. avatarUrl: res.data.url
  260. })
  261. }
  262. })
  263. },
  264. onCustomerService(){
  265. wx.openCustomerServiceChat({
  266. extInfo: {url: 'https://work.weixin.qq.com/kfid/kfcc1bd2a8bddad1dfb'},
  267. corpId: 'wwc4de479c81db4706',
  268. success(res) {}
  269. })
  270. },
  271. /**
  272. * 生命周期函数--监听页面初次渲染完成
  273. */
  274. onReady: function () {
  275. },
  276. /**
  277. * 生命周期函数--监听页面显示
  278. */
  279. onShow: function () {
  280. },
  281. /**
  282. * 生命周期函数--监听页面隐藏
  283. */
  284. onHide: function () {
  285. },
  286. /**
  287. * 生命周期函数--监听页面卸载
  288. */
  289. onUnload: function () {
  290. },
  291. /**
  292. * 页面相关事件处理函数--监听用户下拉动作
  293. */
  294. onPullDownRefresh: function () {
  295. },
  296. /**
  297. * 页面上拉触底事件的处理函数
  298. */
  299. onReachBottom: function () {
  300. },
  301. /**
  302. * 用户点击右上角分享
  303. */
  304. onShareAppMessage: function () {
  305. }
  306. })