电速宝
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // package-user/pages/register/index.js
  2. const api = require('../../../api/index.js');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. items: [
  9. {value: 5, name: '买电'},
  10. {value: 6, name: '卖电'},
  11. ],
  12. fileurl:'',
  13. back:'',
  14. identitycard:'',
  15. greenelectricity:'',
  16. nickname:'',
  17. telephone:'',
  18. powerstationname:''
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad(options) {
  24. },
  25. // 上传
  26. onchooseImage(e) {
  27. this.setData({
  28. identitycard:e.target.dataset.name
  29. })
  30. let _this = this
  31. // 先检查隐私授权状态
  32. wx.getPrivacySetting({
  33. success: (res) => {
  34. if (res.needAuthorization) {
  35. // 弹出隐私协议弹窗
  36. wx.requirePrivacyAuthorize({
  37. success: () => {
  38. console.log('用户已同意隐私协议');
  39. _this.doUploadImage(); // 继续执行上传
  40. },
  41. fail: () => {
  42. console.log('用户拒绝了隐私协议');
  43. wx.showToast({
  44. title: '需同意隐私协议才能使用',
  45. icon: 'none'
  46. });
  47. }
  48. });
  49. } else {
  50. _this.chooseImage(); // 已授权,直接上传
  51. }
  52. }
  53. });
  54. },
  55. // 上传图片
  56. chooseImage() {
  57. let _this = this
  58. wx.chooseMedia({
  59. count: 1,
  60. mediaType: ['image'],
  61. sourceType: ['album', 'camera'],
  62. success(res) {
  63. console.log(res);
  64. const tempFiles = res.tempFiles;
  65. _this.setData({
  66. imageList: tempFiles.map(file => file.tempFilePath),
  67. });
  68. _this.uploadImages(tempFiles);
  69. },
  70. fail(err) {
  71. console.error("选择失败:", err);
  72. }
  73. })
  74. },
  75. radioChange(e) {
  76. console.log('radio发生change事件,携带value值为:', e.detail.value)
  77. // const items = this.data.items
  78. // for (let i = 0, len = items.length; i < len; ++i) {
  79. // items[i].checked = items[i].value === e.detail.value
  80. // }
  81. this.setData({
  82. greenelectricity:e.detail.value
  83. })
  84. console.log(this.data.greenelectricity);
  85. },
  86. bindKeyInput(e){
  87. console.log(e.detail.value);
  88. this.setData({
  89. nickname:e.detail.value
  90. })
  91. },
  92. bindtelephone(e){
  93. console.log(e.detail.value);
  94. this.setData({
  95. telephone:e.detail.value
  96. })
  97. },
  98. bindpowerstationname(e){
  99. console.log(e.detail.value);
  100. this.setData({
  101. powerstationname:e.detail.value
  102. })
  103. },
  104. // 上传到服务器
  105. uploadImages(files) {
  106. let _this =this
  107. const token = wx.getStorageSync('token');
  108. files.forEach((file) => {
  109. wx.uploadFile({
  110. url: 'https://esos-iot.com:9443/system/config/upload/webPost',
  111. filePath: file.tempFilePath,
  112. name: 'file',
  113. formData: {
  114. type:_this.data.identitycard,
  115. front:file.tempFilePath
  116. },
  117. header: {
  118. // 'Authorization': 'Bearer ' + token // Also add to header if needed
  119. },
  120. success: (res) => {
  121. const obj = JSON.parse(res.data);
  122. console.log(obj.data.companyLicense);
  123. _this.setData({
  124. fileurl:obj.data.companyLicense
  125. })
  126. console.log(this.data.fileurl);
  127. },
  128. fail: (err) => {
  129. console.error('上传失败', err);
  130. },
  131. });
  132. });
  133. },
  134. register(){
  135. const phone = this.data.telephone.trim();
  136. const phoneReg = /^1[3-9]\d{9}$/;
  137. console.log(phone);
  138. // 1. 校验是否为空
  139. if (!phone) {
  140. wx.showToast({
  141. title:'请输入手机号',
  142. icon: 'none',
  143. });
  144. return false;
  145. }
  146. // 2. 校验格式是否正确
  147. if (!phoneReg.test(phone)) {
  148. wx.showToast({
  149. title:'请输入正确的手机号',
  150. icon: 'none',
  151. });
  152. return false;
  153. }
  154. if (this.data.nickname==''||this.data.greenelectricity==''||this.data.powerstationname=='') {
  155. wx.showToast({
  156. title:'请填写完整信息',
  157. icon: 'success',
  158. });
  159. return
  160. }
  161. let obj = {
  162. phonecode:this.data.telephone,
  163. operationName:this.data.nickname,
  164. operationRole:this.data.greenelectricity,
  165. companyName:this.data.powerstationname,
  166. companyLicense:this.data.fileurl,
  167. }
  168. api.request(`/sysoperation/registeredusers`, 'post',obj,{ isPublic: true })
  169. .then((data) => {
  170. console.log(data);
  171. if (data.code == 200) {
  172. if (data.msg=='当前用户已注册') {
  173. wx.showToast({
  174. title:data.msg,
  175. icon: 'success',
  176. });
  177. return
  178. }
  179. wx.showToast({
  180. title:data.msg,
  181. icon: 'success',
  182. });
  183. wx.navigateTo({
  184. url: '/package-user/pages/login/index', // 登录页面的路径,注意路径前面加上 '/' 表示从根目录开始
  185. });
  186. }else{
  187. wx.showToast({
  188. title: data.msg,
  189. icon: 'success',
  190. });
  191. }
  192. })
  193. .catch((err) => {
  194. wx.showToast({
  195. title:'注册失败',
  196. icon: 'none',
  197. });
  198. console.error('请求失败:', err);
  199. });
  200. },
  201. previewSingleImage(e) {
  202. // 获取大图路径
  203. const largePicUrl = e.currentTarget.dataset.url;
  204. // 调用API预览大图
  205. wx.previewImage({
  206. current: largePicUrl, // 当前展示的大图路径
  207. urls: [largePicUrl] // 需预览的图片数组,单图时数组中仅存该图路径
  208. });
  209. },
  210. /**
  211. * 生命周期函数--监听页面初次渲染完成
  212. */
  213. onReady() {
  214. },
  215. /**
  216. * 生命周期函数--监听页面显示
  217. */
  218. onShow() {
  219. },
  220. /**
  221. * 生命周期函数--监听页面隐藏
  222. */
  223. onHide() {
  224. },
  225. /**
  226. * 生命周期函数--监听页面卸载
  227. */
  228. onUnload() {
  229. },
  230. /**
  231. * 页面相关事件处理函数--监听用户下拉动作
  232. */
  233. onPullDownRefresh() {
  234. },
  235. /**
  236. * 页面上拉触底事件的处理函数
  237. */
  238. onReachBottom() {
  239. },
  240. /**
  241. * 用户点击右上角分享
  242. */
  243. onShareAppMessage() {
  244. }
  245. })