user.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. $api.login(udata)
  208. .then(res=>{
  209. //登录成功
  210. wx.hideLoading();
  211. if (res.data.code != 0) {
  212. wx.showToast({
  213. icon: 'none',
  214. title: res.data.message,
  215. })
  216. return;
  217. }
  218. wx.setStorage({
  219. key: 'userInfo',
  220. data: res.data.data,
  221. })
  222. //老用户直接进入系统
  223. if(res.data.data.role != 0){
  224. app.globalData.follow = 1
  225. wx.switchTab({
  226. url: '../index/index',
  227. })
  228. return;
  229. }
  230. //新用户选择报名
  231. _this.setData({
  232. status:3,
  233. player_type:0
  234. })
  235. wx.setNavigationBarTitle({
  236. title: '',
  237. })
  238. })
  239. .catch(err=>{
  240. wx.hideLoading();
  241. })
  242. },
  243. login1(){
  244. logindata.phone=this.data.phone
  245. logindata.phcode=this.data.phcode
  246. if (logindata.phone.length<11){
  247. wx.showToast({
  248. icon: 'none',
  249. title: '请输入正确的手机号',
  250. })
  251. return
  252. }
  253. if (!logindata.phcode) {
  254. wx.showToast({
  255. icon: 'none',
  256. title: '请输入验证码',
  257. })
  258. return
  259. }
  260. $api.login(logindata).then(res=>{
  261. if(res.data.code == 0){
  262. this.setData({
  263. status:3
  264. })
  265. }
  266. })
  267. },
  268. login2(){
  269. if (this.data.phone.length<11) {
  270. wx.showToast({
  271. icon: 'none',
  272. title: '请输入正确的手机号',
  273. })
  274. return
  275. }
  276. if (!this.data.phcode) {
  277. wx.showToast({
  278. icon: 'none',
  279. title: '请输入验证码',
  280. })
  281. return
  282. }
  283. $api.bindphone({phone:this.data.phone,phcode:this.data.phcode}).then(res=>{
  284. if(res.data.code == 0){
  285. this.setData({
  286. status:3
  287. })
  288. }
  289. })
  290. },
  291. uploadImg(path){
  292. $api.uploadAvatar(path,'file').then(res=>{
  293. if(res.code == 0){
  294. this.setData({
  295. avatarUrl: res.data.url
  296. })
  297. }
  298. })
  299. },
  300. onCustomerService(){
  301. wx.openCustomerServiceChat({
  302. extInfo: {url: 'https://work.weixin.qq.com/kfid/kfcc1bd2a8bddad1dfb'},
  303. corpId: 'wwc4de479c81db4706',
  304. success(res) {}
  305. })
  306. },
  307. /**
  308. * 生命周期函数--监听页面初次渲染完成
  309. */
  310. onReady: function () {
  311. },
  312. /**
  313. * 生命周期函数--监听页面显示
  314. */
  315. onShow: function () {
  316. },
  317. /**
  318. * 生命周期函数--监听页面隐藏
  319. */
  320. onHide: function () {
  321. },
  322. /**
  323. * 生命周期函数--监听页面卸载
  324. */
  325. onUnload: function () {
  326. },
  327. /**
  328. * 页面相关事件处理函数--监听用户下拉动作
  329. */
  330. onPullDownRefresh: function () {
  331. },
  332. /**
  333. * 页面上拉触底事件的处理函数
  334. */
  335. onReachBottom: function () {
  336. },
  337. /**
  338. * 用户点击右上角分享
  339. */
  340. onShareAppMessage: function () {
  341. }
  342. })