user.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. $api.contentCheck({content:this.data.nickName,openid:this.data.openid}).then(res=>{
  136. var result = res.data.data.result
  137. if(result.suggest == 'pass'){
  138. this.wxlogin()
  139. } else{
  140. wx.showToast({
  141. icon: 'none',
  142. title: '昵称含违规信息'
  143. })
  144. }
  145. })
  146. },
  147. islogin(){
  148. var _this=this;
  149. wx.showLoading({
  150. title: '正在登陆',
  151. })
  152. wx.login({
  153. success(res) {
  154. //获取code
  155. $api.getOpenid({ code: res.code})
  156. .then(res => {
  157. //获取openid
  158. if (res.data.code != 0) {
  159. wx.showToast({
  160. icon: 'none',
  161. title: res.data.message,
  162. })
  163. wx.hideLoading();
  164. return;
  165. }
  166. var nopenid = res.data.data.openid
  167. var nunionid = res.data.data.unionid
  168. _this.setData({
  169. openid:nopenid,
  170. unionid:nunionid,
  171. })
  172. // console.log(res.data.data.unionid,222)
  173. $api.isUserLogin({openid:nopenid,unionid:nunionid})
  174. .then(res=>{
  175. wx.hideLoading();
  176. if (res.data.code != 0) {
  177. wx.showToast({
  178. icon: 'none',
  179. title: res.data.message,
  180. })
  181. return;
  182. }
  183. //有登录
  184. if(res.data.data.id){
  185. _this.setData({
  186. nickName:res.data.data.username,
  187. avatarUrl:res.data.data.avatar,
  188. })
  189. _this.wxlogin()
  190. } else{
  191. _this.setData({
  192. status:10,
  193. })
  194. }
  195. })
  196. .catch(err=>{
  197. wx.hideLoading();
  198. })
  199. })
  200. .catch(err => {
  201. //请求失败
  202. wx.hideLoading();
  203. })
  204. }
  205. })
  206. },
  207. wxlogin(){
  208. var _this=this;
  209. wx.showLoading({
  210. title: '正在登陆',
  211. })
  212. var udata = {};
  213. udata.openid = this.data.openid;
  214. udata.unionid = this.data.unionid;
  215. udata.nickName = this.data.nickName;
  216. udata.avatarUrl = this.data.avatarUrl;
  217. udata.logintype = 'xcx';
  218. // console.log(udata)
  219. $api.login(udata)
  220. .then(res=>{
  221. //登录成功
  222. wx.hideLoading();
  223. if (res.data.code != 0) {
  224. wx.showToast({
  225. icon: 'none',
  226. title: res.data.message,
  227. })
  228. return;
  229. }
  230. // console.log(res.data.data,'user')
  231. wx.setStorage({
  232. key: 'userInfo',
  233. data: res.data.data,
  234. })
  235. wx.setStorage({
  236. key: 'user',
  237. data: res.data.data,
  238. })
  239. //老用户直接进入系统
  240. if(res.data.data.role != 0){
  241. app.globalData.follow = 1
  242. wx.switchTab({
  243. url: '../index/index',
  244. })
  245. } else{
  246. wx.navigateBack()
  247. }
  248. // return;
  249. // }
  250. // //新用户选择报名
  251. // _this.setData({
  252. // status:3,
  253. // player_type:0
  254. // })
  255. wx.setNavigationBarTitle({
  256. title: '',
  257. })
  258. })
  259. .catch(err=>{
  260. wx.hideLoading();
  261. })
  262. },
  263. login1(){
  264. logindata.phone=this.data.phone
  265. logindata.phcode=this.data.phcode
  266. if (logindata.phone.length<11){
  267. wx.showToast({
  268. icon: 'none',
  269. title: '请输入正确的手机号',
  270. })
  271. return
  272. }
  273. if (!logindata.phcode) {
  274. wx.showToast({
  275. icon: 'none',
  276. title: '请输入验证码',
  277. })
  278. return
  279. }
  280. $api.login(logindata).then(res=>{
  281. if(res.data.code == 0){
  282. this.setData({
  283. status:3
  284. })
  285. }
  286. })
  287. },
  288. login2(){
  289. if (this.data.phone.length<11) {
  290. wx.showToast({
  291. icon: 'none',
  292. title: '请输入正确的手机号',
  293. })
  294. return
  295. }
  296. if (!this.data.phcode) {
  297. wx.showToast({
  298. icon: 'none',
  299. title: '请输入验证码',
  300. })
  301. return
  302. }
  303. $api.bindphone({phone:this.data.phone,phcode:this.data.phcode}).then(res=>{
  304. if(res.data.code == 0){
  305. this.setData({
  306. status:3
  307. })
  308. }
  309. })
  310. },
  311. uploadImg(path){
  312. $api.uploadAvatar(path,'file').then(res=>{
  313. if(res.code == 0){
  314. this.setData({
  315. avatarUrl: res.data.url
  316. })
  317. }
  318. })
  319. },
  320. cancel(){
  321. wx.navigateBack();
  322. },
  323. onCustomerService(){
  324. wx.openCustomerServiceChat({
  325. extInfo: {url: 'https://work.weixin.qq.com/kfid/kfcc1bd2a8bddad1dfb'},
  326. corpId: 'wwc4de479c81db4706',
  327. success(res) {}
  328. })
  329. },
  330. pushMessage(e){
  331. if($api.checkUser()){
  332. wx.navigateTo({
  333. url: '../message/message',
  334. })
  335. }
  336. },
  337. /**
  338. * 生命周期函数--监听页面初次渲染完成
  339. */
  340. onReady: function () {
  341. },
  342. /**
  343. * 生命周期函数--监听页面显示
  344. */
  345. onShow: function () {
  346. },
  347. /**
  348. * 生命周期函数--监听页面隐藏
  349. */
  350. onHide: function () {
  351. },
  352. /**
  353. * 生命周期函数--监听页面卸载
  354. */
  355. onUnload: function () {
  356. },
  357. /**
  358. * 页面相关事件处理函数--监听用户下拉动作
  359. */
  360. onPullDownRefresh: function () {
  361. },
  362. /**
  363. * 页面上拉触底事件的处理函数
  364. */
  365. onReachBottom: function () {
  366. },
  367. /**
  368. * 用户点击右上角分享
  369. */
  370. onShareAppMessage: function () {
  371. }
  372. })