user.js 7.3 KB

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