User.php 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use think\Db;
  10. use wxpay;
  11. /**
  12. * 会员接口
  13. */
  14. class User extends Api
  15. {
  16. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'registercheck', 'resetpwd', 'changeemail', 'changemobile', 'third', 'getopenid', 'getagreement', 'wxlogin'];
  17. protected $noNeedRight = '*';
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. if (!Config::get('fastadmin.usercenter')) {
  22. $this->error(__('User center already closed'));
  23. }
  24. }
  25. /**
  26. * 会员中心
  27. */
  28. public function index()
  29. {
  30. $this->success('', ['welcome' => $this->auth->nickname]);
  31. }
  32. /**
  33. * 会员登录
  34. *
  35. * @ApiMethod (POST)
  36. * @param string $account 账号
  37. * @param string $password 密码
  38. */
  39. public function login()
  40. {
  41. $account = $this->request->post('account');
  42. $password = $this->request->post('password');
  43. if (!$account || !$password) {
  44. $this->error(__('Invalid parameters'));
  45. }
  46. $ret = $this->auth->login($account, $password);
  47. if ($ret) {
  48. $data = ['userinfo' => $this->auth->getUserinfo()];
  49. $this->success(__('Logged in successful'), $data);
  50. } else {
  51. $this->error($this->auth->getError());
  52. }
  53. }
  54. /**
  55. * 手机验证码登录
  56. *
  57. * @ApiMethod (POST)
  58. * @param string $mobile 手机号
  59. * @param string $captcha 验证码
  60. */
  61. public function mobilelogin()
  62. {
  63. $mobile = $this->request->post('mobile');
  64. $captcha = $this->request->post('captcha');
  65. if (!$mobile || !$captcha) {
  66. $this->error(__('Invalid parameters'));
  67. }
  68. if (!Validate::regex($mobile, "^1\d{10}$")) {
  69. $this->error(__('Mobile is incorrect'));
  70. }
  71. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  72. $this->error(__('Captcha is incorrect'));
  73. }
  74. $user = \app\common\model\User::getByMobile($mobile);
  75. if (!$user) {
  76. $this->error('用户尚未注册');
  77. }
  78. if ($user['status'] != 1) {
  79. $this->error(__('Account is locked'));
  80. }
  81. // if ($user) {
  82. // if ($user->status != 'normal') {
  83. // $this->error(__('Account is locked'));
  84. // }
  85. // //如果已经有账号则直接登录
  86. // $ret = $this->auth->direct($user->id);
  87. // } else {
  88. // $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  89. // }
  90. $ret = $this->auth->direct($user->id);
  91. if ($ret) {
  92. Sms::flush($mobile, 'mobilelogin');
  93. $data = ['userinfo' => $this->auth->getUserinfo()];
  94. $this->success(__('Logged in successful'), $data);
  95. } else {
  96. $this->error($this->auth->getError());
  97. }
  98. }
  99. /**
  100. * 注册会员
  101. *
  102. * @ApiMethod (POST)
  103. * @param string $username 用户名
  104. * @param string $password 密码
  105. * @param string $email 邮箱
  106. * @param string $mobile 手机号
  107. * @param string $code 验证码
  108. */
  109. /*public function register()
  110. {
  111. $mobile = $this->request->post('mobile', '', 'trim'); //手机号
  112. $code = $this->request->post('code', '', 'trim'); //验证码
  113. $password = $this->request->post('password', '' , 'trim'); //密码
  114. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  115. $nickname = $this->request->post('nickname', '', 'trim'); //姓名
  116. $idcard = $this->request->post('idcard', '', 'trim,strip_tags,htmlspecialchars'); //身份证号
  117. $province = $this->request->post('province', '', 'trim'); //省
  118. $city = $this->request->post('city', '', 'trim'); //市
  119. $area = $this->request->post('area', '', 'trim'); //区
  120. $address = $this->request->post('address', '', 'trim'); //详细地址
  121. $recommender = $this->request->post('recommender', '', 'trim'); //推荐人姓名
  122. $recommender_mobile = $this->request->post('recommender_mobile', '', 'trim'); //推荐人手机号
  123. $zimage = $this->request->post('zimage', '', 'trim,strip_tags,htmlspecialchars'); //身份证正面照
  124. $fimage = $this->request->post('fimage', '', 'trim,strip_tags,htmlspecialchars'); //身份证反面照
  125. if (!Validate::regex($mobile, "^1\d{10}$")) {
  126. $this->error(__('Mobile is incorrect'));
  127. }
  128. $count = Db::name('user')->where(['mobile' => $mobile])->count('id');
  129. if ($count) {
  130. $this->error('手机号已被注册');
  131. }
  132. if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  133. $this->error(__('Captcha is incorrect'));
  134. }
  135. $ret = Sms::check($mobile, $code, 'register');
  136. if (!$ret) {
  137. $this->error(__('Captcha is incorrect'));
  138. }
  139. if (!$password) {
  140. $this->error('请输入新密码');
  141. }
  142. //验证新密码
  143. if (!Validate::make()->check(['newpassword' => $password], ['newpassword' => 'require|regex:\S{6,30}'])) {
  144. $this->error(__('Password must be 6 to 30 characters'));
  145. }
  146. if (!$repassword) {
  147. $this->error('请再次输入密码');
  148. }
  149. if ($repassword != $password) {
  150. $this->error('两次密码不一致');
  151. }
  152. if (!$nickname) {
  153. $this->error('请输入姓名');
  154. }
  155. if (iconv_strlen($nickname, 'utf-8') > 30) {
  156. $this->error('姓名最多30位');
  157. }
  158. if (!$idcard) {
  159. $this->error('请输入身份证号');
  160. }
  161. if (iconv_strlen($idcard, 'utf-8') != 18) {
  162. $this->error('身份证号错误');
  163. }
  164. if (!$province || !$city || !$area) {
  165. $this->error('请选择养殖场地址');
  166. }
  167. if (!$address) {
  168. $this->error('请输入详细地址');
  169. }
  170. if (iconv_strlen($address, 'utf-8') > 255) {
  171. $this->error('详细地址最多255位');
  172. }
  173. if (!$recommender) {
  174. $this->error('请输入推荐人姓名');
  175. }
  176. if (iconv_strlen($recommender, 'utf-8') > 30) {
  177. $this->error('推荐人姓名最多30位');
  178. }
  179. if (!$recommender_mobile || !is_mobile($recommender_mobile)) {
  180. $this->error('请输入正确推荐人手机号');
  181. }
  182. if (!$zimage || !$fimage) {
  183. $this->error('请上传身份证相关照片');
  184. }
  185. $ip = request()->ip();
  186. $time = time();
  187. $data = [
  188. 'nickname' => $nickname,
  189. 'province' => $province,
  190. 'city' => $city,
  191. 'area' => $area,
  192. 'address' => $address,
  193. 'createtime' => $time
  194. ];
  195. $params = array_merge($data, [
  196. 'mobile' => $mobile,
  197. 'password' => $password,
  198. 'avatar' => '/assets/img/avatar.png',
  199. 'salt' => Random::alnum(),
  200. 'jointime' => $time,
  201. 'joinip' => $ip,
  202. 'logintime' => $time,
  203. 'loginip' => $ip,
  204. 'prevtime' => $time,
  205. 'is_auth' => 1
  206. ]);
  207. $params['password'] = md5(md5($password) . $params['salt']);
  208. //开启事务
  209. Db::startTrans();
  210. $rs = Db::name('user')->insertGetId($params);
  211. if (!$rs) {
  212. Db::rollback();
  213. $this->error('注册失败');
  214. }
  215. $data['user_id'] = $rs;
  216. $data['idcard'] = $idcard;
  217. $data['zimage'] = $zimage;
  218. $data['fimage'] = $fimage;
  219. $data['recommender'] = $recommender;
  220. $data['recommender_mobile'] = $recommender_mobile;
  221. $rt = Db::name('user_auth')->insertGetId($data);
  222. if (!$rt) {
  223. Db::rollback();
  224. $this->error('注册失败');
  225. }
  226. Db::commit();
  227. $ret = $this->auth->login($mobile, $password);
  228. if ($ret) {
  229. $data = ['userinfo' => $this->auth->getUserinfo()];
  230. $this->success(__('Sign up successful'), $data);
  231. } else {
  232. $this->error($this->auth->getError());
  233. }
  234. }*/
  235. //注册第一步验证
  236. public function registercheck()
  237. {
  238. $mobile = $this->request->post('mobile'); //手机号
  239. $code = $this->request->post('code'); //验证码
  240. $password = $this->request->post('password'); //密码
  241. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  242. if (!Validate::regex($mobile, "^1\d{10}$")) {
  243. $this->error(__('Mobile is incorrect'));
  244. }
  245. $count = Db::name('user')->where(['mobile' => $mobile])->count('id');
  246. if (!$count) {
  247. $this->error('手机号已被注册');
  248. }
  249. if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  250. $this->error(__('Captcha is incorrect'));
  251. }
  252. $ret = Sms::check($mobile, $code, 'register');
  253. if (!$ret) {
  254. $this->error(__('Captcha is incorrect'));
  255. }
  256. if (!$password) {
  257. $this->error('请输入新密码');
  258. }
  259. //验证新密码
  260. if (!Validate::make()->check(['newpassword' => $password], ['newpassword' => 'require|regex:\S{6,30}'])) {
  261. $this->error(__('Password must be 6 to 30 characters'));
  262. }
  263. if (!$repassword) {
  264. $this->error('请再次输入密码');
  265. }
  266. if ($repassword != $password) {
  267. $this->error('两次密码不一致');
  268. }
  269. $this->success('验证通过');
  270. }
  271. /**
  272. * 退出登录
  273. * @ApiMethod (POST)
  274. */
  275. public function logout()
  276. {
  277. if (!$this->request->isPost()) {
  278. $this->error(__('Invalid parameters'));
  279. }
  280. $this->auth->logout();
  281. $this->success(__('Logout successful'));
  282. }
  283. //查询会员信息
  284. public function getinfo()
  285. {
  286. //检查今日是否登录赠送过成长值
  287. $this->checklogingrowth($this->auth->id);
  288. //检查会员等级
  289. $this->checkviplevel($this->auth->id);
  290. $user = Db::name('user')->find($this->auth->id);
  291. $data['nickname'] = $user['nickname']; //姓名
  292. $data['username'] = $user['username']; //UID
  293. $data['avatar'] = cdnurl($user['avatar']); //头像
  294. $data['mobile'] = $user['mobile']; //手机号
  295. $data['money'] = $user['money']; //余额
  296. $data['realname'] = $user['realname']; //真实姓名
  297. $data['gender'] = $user['gender']; //性别:1=男,2=女
  298. $data['birthday'] = date('Y-m-d', $user['birthday']); //生日
  299. $data['idcard'] = $user['idcard']; //身份证号
  300. $data['passport'] = $user['passport']; //护照号
  301. $data['emergencycontact'] = $user['emergencycontact']; //紧急联系人
  302. $data['contactmobile'] = $user['contactmobile']; //紧急联系方式
  303. $data['outdoorduration'] = $user['outdoorduration']; //户外时长
  304. $data['maxlevel'] = $user['maxlevel']; //实际有效会员ID
  305. $data['viplevel'] = Db::name('vip')->where(['id' => $user['maxlevel']])->value('level'); //会员等级
  306. $data['active_count'] = Db::name('active_order')->where(['user_id' => $this->auth->id, 'status' => 2])->count('id'); //完成活动数量
  307. $data['freenumber'] = $user['freenumber']; //会员免费参加活动次数
  308. $this->success('会员信息', $data);
  309. }
  310. /**
  311. * 修改会员个人信息
  312. *
  313. * @ApiMethod (POST)
  314. * @param string $avatar 头像地址
  315. * @param string $username 用户名
  316. * @param string $nickname 昵称
  317. * @param string $bio 个人简介
  318. */
  319. public function profile()
  320. {
  321. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars'); //头像
  322. $nickname = $this->request->post('nickname', '', 'trim'); //昵称
  323. $realname = $this->request->post('realname', '', 'trim'); //真实姓名
  324. $idcard = $this->request->post('idcard', '', 'trim,strip_tags,htmlspecialchars'); //身份证号
  325. $passport = $this->request->post('passport', '', 'trim,strip_tags,htmlspecialchars'); //护照号
  326. $emergencycontact = $this->request->post('emergencycontact', '', 'trim'); //紧急联系人
  327. $contactmobile = $this->request->post('contactmobile', '', 'trim'); //紧急联系方式
  328. $outdoorduration = $this->request->post('outdoorduration', '', 'trim'); //户外时长
  329. $birthday = $this->request->post('birthday', '', 'strtotime'); //生日
  330. $gender = $this->request->post('gender', 0, 'intval'); //性别
  331. $data = [];
  332. if ($avatar) {
  333. if (iconv_strlen($avatar, 'utf-8') > 255) {
  334. $this->error('图片超出范围');
  335. }
  336. $data['avatar'] = $avatar;
  337. }
  338. if ($nickname) {
  339. if (iconv_strlen($nickname, 'utf-8') > 30) {
  340. $this->error('昵称最多30字');
  341. }
  342. $data['nickname'] = $nickname;
  343. }
  344. if ($realname) {
  345. if(iconv_strlen($realname, 'utf-8') > 30) {
  346. $this->error('真实姓名最多30字');
  347. }
  348. $data['realname'] = $realname;
  349. }
  350. if ($idcard) {
  351. if (iconv_strlen($idcard, 'utf-8') != 18) {
  352. $this->error('身份证号错误');
  353. }
  354. $data['idcard'] = $idcard;
  355. }
  356. if ($passport) {
  357. if (iconv_strlen($passport, 'utf-8') > 30) {
  358. $this->error('护照号错误');
  359. }
  360. $data['passport'] = $passport;
  361. }
  362. if ($emergencycontact) {
  363. if(iconv_strlen($emergencycontact, 'utf-8') > 30) {
  364. $this->error('紧急联系人最多30字');
  365. }
  366. $data['emergencycontact'] = $emergencycontact;
  367. }
  368. if ($contactmobile) {
  369. if(!is_mobile($contactmobile)) {
  370. $this->error('请输入正确紧急联系方式');
  371. }
  372. $data['contactmobile'] = $contactmobile;
  373. }
  374. if ($outdoorduration) {
  375. if(iconv_strlen($outdoorduration, 'utf-8') > 50) {
  376. $this->error('户外时长最多50字');
  377. }
  378. $data['outdoorduration'] = $outdoorduration;
  379. }
  380. if ($birthday) {
  381. $data['birthday'] = $birthday;
  382. }
  383. if (in_array($gender, [1, 2])) {
  384. $data['gender'] = $gender;
  385. }
  386. // $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  387. // if ($username) {
  388. // $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  389. // if ($exists) {
  390. // $this->error(__('Username already exists'));
  391. // }
  392. // $user->username = $username;
  393. // }
  394. // if ($nickname) {
  395. // $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  396. // if ($exists) {
  397. // $this->error(__('Nickname already exists'));
  398. // }
  399. // $user->nickname = $nickname;
  400. // }
  401. // $user->avatar = $avatar;
  402. if (!$data) {
  403. $this->error('暂无修改内容');
  404. }
  405. //修改用户表
  406. $rt = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  407. if ($rt === false) {
  408. $this->error('修改失败');
  409. }
  410. $this->success('修改成功');
  411. }
  412. //修改头像
  413. public function changeavatar()
  414. {
  415. $avatar = input('avatar', '', 'trim'); //头像
  416. if (!$avatar) {
  417. $this->error('请上传头像');
  418. }
  419. if (iconv_strlen($avatar, 'utf-8') > 255) {
  420. $this->error('头像长度超出限制');
  421. }
  422. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField('avatar', $avatar);
  423. if (!$rs) {
  424. $this->error('修改失败');
  425. }
  426. $this->success('修改成功');
  427. }
  428. /**
  429. * 修改邮箱
  430. *
  431. * @ApiMethod (POST)
  432. * @param string $email 邮箱
  433. * @param string $captcha 验证码
  434. */
  435. public function changeemail()
  436. {
  437. $user = $this->auth->getUser();
  438. $email = $this->request->post('email');
  439. $captcha = $this->request->post('captcha');
  440. if (!$email || !$captcha) {
  441. $this->error(__('Invalid parameters'));
  442. }
  443. if (!Validate::is($email, "email")) {
  444. $this->error(__('Email is incorrect'));
  445. }
  446. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  447. $this->error(__('Email already exists'));
  448. }
  449. $result = Ems::check($email, $captcha, 'changeemail');
  450. if (!$result) {
  451. $this->error(__('Captcha is incorrect'));
  452. }
  453. $verification = $user->verification;
  454. $verification->email = 1;
  455. $user->verification = $verification;
  456. $user->email = $email;
  457. $user->save();
  458. Ems::flush($email, 'changeemail');
  459. $this->success();
  460. }
  461. /**
  462. * 修改手机号
  463. *
  464. * @ApiMethod (POST)
  465. * @param string $mobile 手机号
  466. * @param string $captcha 验证码
  467. */
  468. public function changemobile()
  469. {
  470. $user = $this->auth->getUser();
  471. $code = $this->request->post('code');
  472. $mobile = $this->request->post('mobile');
  473. $captcha = $this->request->post('captcha');
  474. if (!$code || !$mobile || !$captcha) {
  475. $this->error(__('Invalid parameters'));
  476. }
  477. if ($mobile == $user->mobile) {
  478. $this->error('手机号没有改变');
  479. }
  480. $result = Sms::check($user->mobile, $code, 'changeyuanmobile');
  481. if (!$result) {
  482. $this->error(__('Captcha is incorrect'));
  483. }
  484. if (!Validate::regex($mobile, "^1\d{10}$")) {
  485. $this->error(__('Mobile is incorrect'));
  486. }
  487. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  488. $this->error(__('手机号已存在'));
  489. }
  490. $result = Sms::check($mobile, $captcha, 'changemobile');
  491. if (!$result) {
  492. $this->error(__('Captcha is incorrect'));
  493. }
  494. $verification = $user->verification;
  495. $verification->mobile = 1;
  496. $user->verification = $verification;
  497. $user->mobile = $mobile;
  498. $user->save();
  499. Sms::flush($user->mobile, 'changemobile');
  500. Sms::flush($mobile, 'changemobile');
  501. $this->success('修改成功');
  502. }
  503. /**
  504. * 第三方登录
  505. *
  506. * @ApiMethod (POST)
  507. * @param string $platform 平台名称
  508. * @param string $code Code码
  509. */
  510. public function third()
  511. {
  512. $url = url('user/index');
  513. $platform = $this->request->post("platform");
  514. $code = $this->request->post("code");
  515. $config = get_addon_config('third');
  516. if (!$config || !isset($config[$platform])) {
  517. $this->error(__('Invalid parameters'));
  518. }
  519. $app = new \addons\third\library\Application($config);
  520. //通过code换access_token和绑定会员
  521. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  522. if ($result) {
  523. $loginret = \addons\third\library\Service::connect($platform, $result);
  524. if ($loginret) {
  525. $data = [
  526. 'userinfo' => $this->auth->getUserinfo(),
  527. 'thirdinfo' => $result
  528. ];
  529. $this->success(__('Logged in successful'), $data);
  530. }
  531. }
  532. $this->error(__('Operation failed'), $url);
  533. }
  534. /**
  535. * 重置密码
  536. *
  537. * @ApiMethod (POST)
  538. * @param string $mobile 手机号
  539. * @param string $newpassword 新密码
  540. * @param string $captcha 验证码
  541. */
  542. public function resetpwd()
  543. {
  544. $type = 'mobile';//$this->request->post("type");
  545. $mobile = $this->request->post("mobile");
  546. $email = $this->request->post("email");
  547. $newpassword = $this->request->post("newpassword");
  548. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  549. $captcha = $this->request->post("captcha");
  550. if (!$newpassword || !$captcha || !$repassword) {
  551. $this->error(__('Invalid parameters'));
  552. }
  553. //验证Token
  554. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  555. $this->error(__('Password must be 6 to 30 characters'));
  556. }
  557. if ($repassword != $newpassword) {
  558. $this->error('两次密码不一致');
  559. }
  560. if ($type == 'mobile') {
  561. if (!Validate::regex($mobile, "^1\d{10}$")) {
  562. $this->error(__('Mobile is incorrect'));
  563. }
  564. $user = \app\common\model\User::getByMobile($mobile);
  565. if (!$user) {
  566. $this->error(__('User not found'));
  567. }
  568. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  569. if (!$ret) {
  570. $this->error(__('Captcha is incorrect'));
  571. }
  572. Sms::flush($mobile, 'resetpwd');
  573. } else {
  574. if (!Validate::is($email, "email")) {
  575. $this->error(__('Email is incorrect'));
  576. }
  577. $user = \app\common\model\User::getByEmail($email);
  578. if (!$user) {
  579. $this->error(__('User not found'));
  580. }
  581. $ret = Ems::check($email, $captcha, 'resetpwd');
  582. if (!$ret) {
  583. $this->error(__('Captcha is incorrect'));
  584. }
  585. Ems::flush($email, 'resetpwd');
  586. }
  587. //模拟一次登录
  588. $this->auth->direct($user->id);
  589. $ret = $this->auth->changepwd($newpassword, '', true);
  590. if ($ret) {
  591. $this->success(__('Reset password successful'));
  592. } else {
  593. $this->error($this->auth->getError());
  594. }
  595. }
  596. /**
  597. * 修改密码
  598. *
  599. * @ApiMethod (POST)
  600. * @param string $captcha 验证码
  601. * @param string $newpassword 新密码
  602. * @param string $repassword 确认密码
  603. */
  604. public function editpwd()
  605. {
  606. $captcha = $this->request->post("captcha", '', 'trim'); //验证码
  607. $newpassword = $this->request->post("newpassword", '' , 'trim'); //新密码
  608. $repassword = $this->request->post('repassword', '' , 'trim'); //确认密码
  609. if (iconv_strlen($captcha, 'utf-8') != config('alisms.length')) {
  610. $this->error(__('Captcha is incorrect'));
  611. }
  612. if (!$newpassword) {
  613. $this->error('请输入新密码');
  614. }
  615. //验证新密码
  616. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  617. $this->error(__('Password must be 6 to 30 characters'));
  618. }
  619. if (!$repassword) {
  620. $this->error('请再次输入密码');
  621. }
  622. if ($repassword != $newpassword) {
  623. $this->error('两次密码不一致');
  624. }
  625. // $user = \app\common\model\User::getById($this->auth->id);
  626. // if (!$user) {
  627. // $this->error(__('User not found'));
  628. // }
  629. $ret = Sms::check($this->auth->mobile, $captcha, 'changepwd');
  630. if (!$ret) {
  631. $this->error(__('Captcha is incorrect'));
  632. }
  633. //模拟一次登录
  634. $this->auth->direct($this->auth->id);
  635. $ret = $this->auth->changepwd($newpassword, '', true);
  636. if ($ret) {
  637. Sms::flush($this->auth->mobile, 'changepwd');
  638. $this->success(__('Reset password successful'));
  639. } else {
  640. $this->error($this->auth->getError());
  641. }
  642. }
  643. //查询经营信息
  644. public function get_businessinfo()
  645. {
  646. $data = Db::name('user_business_info')->field('manage, mobile, province, city, area, distribution_channel')
  647. ->where(['user_id' => $this->auth->id])->find();
  648. $this->success('经营信息', $data);
  649. }
  650. //修改经营信息
  651. public function editbusinessinfo()
  652. {
  653. $manage = $this->request->post('manage', '', 'trim'); //采购负责人
  654. $mobile = $this->request->post('mobile', '', 'trim'); //联系方式
  655. $province = $this->request->post('province', '', 'trim'); //省
  656. $city = $this->request->post('city', '', 'trim'); //市
  657. $area = $this->request->post('area', '', 'trim'); //区
  658. $distribution_channel = $this->request->post('distribution_channel', '', 'trim'); //销售渠道
  659. $data = [];
  660. if ($manage) {
  661. if (iconv_strlen($manage, 'utf-8') > 50) {
  662. $this->error('采购负责人最多50字');
  663. }
  664. $data['manage'] = $manage;
  665. }
  666. if ($mobile) {
  667. if (!Validate::regex($mobile, "^1\d{10}$")) {
  668. $this->error(__('Mobile is incorrect'));
  669. }
  670. $data['mobile'] = $mobile;
  671. }
  672. if ($province && $city && $area) {
  673. $data['province'] = $province;
  674. $data['city'] = $city;
  675. $data['area'] = $area;
  676. }
  677. if ($distribution_channel) {
  678. if (iconv_strlen($distribution_channel, 'utf-8') > 255) {
  679. $this->error('销售渠道说明最多50字');
  680. }
  681. $data['distribution_channel'] = $distribution_channel;
  682. }
  683. if (!$data) {
  684. $this->error('暂无修改内容');
  685. }
  686. $count = Db::name('user_business_info')->where(['user_id' => $this->auth->id])->count();
  687. if ($count) {
  688. $data['updatetime'] = time();
  689. $rs = Db::name('user_business_info')->where(['user_id' => $this->auth->id])->setField($data);
  690. } else {
  691. $data['user_id'] = $this->auth->id;
  692. $data['createtime'] = time();
  693. $rs = Db::name('user_business_info')->insertGetId($data);
  694. }
  695. if (!$rs) {
  696. $this->error('修改失败');
  697. }
  698. $this->success('修改成功');
  699. }
  700. //查询猪车信息
  701. public function getpigcar()
  702. {
  703. $data = Db::name('user_pig_car')->where(['user_id' => $this->auth->id])->select();
  704. $data = list_domain_image($data, ['images']);
  705. $this->success('查询猪车信息', $data);
  706. }
  707. /**
  708. * 添加猪车信息
  709. *
  710. *$data = [
  711. [
  712. 'carnumber' => '123',
  713. 'carframenumber' => '344',
  714. 'images' => '123.jpg,234.jpg,345.jpg'
  715. ],
  716. [
  717. 'carnumber' => '123',
  718. 'carframenumber' => '344',
  719. 'images' => '123.jpg,234.jpg,345.jpg'
  720. ],
  721. [
  722. 'carnumber' => '123',
  723. 'carframenumber' => '344',
  724. 'images' => '123.jpg,234.jpg,345.jpg'
  725. ]
  726. ];
  727. *
  728. */
  729. public function addpigcar()
  730. {
  731. $pig_car = input('pig_car', '', 'trim'); //猪车信息json串: carnumber车牌号 carframenumber车架号 images车辆照片
  732. if (!$pig_car) {
  733. $this->error('请添加猪车信息');
  734. }
  735. $pig_car = json_decode($pig_car, true);
  736. if (!$pig_car) {
  737. $this->error('请添加猪车信息');
  738. }
  739. if (count($pig_car) > 9) {
  740. $this->error('一次最多添加9条记录');
  741. }
  742. $_data = [];
  743. foreach ($pig_car as &$v) {
  744. $data = [];
  745. if (!$v['carnumber']) {
  746. $this->error('请输入车牌号');
  747. break;
  748. }
  749. if (iconv_strlen($v['carnumber'], 'utf-8') > 50) {
  750. $this->error('车牌号最多50字');
  751. break;
  752. }
  753. if (!$v['carframenumber']) {
  754. $this->error('请输入车架号');
  755. break;
  756. }
  757. if (iconv_strlen($v['carframenumber'], 'utf-8') > 50) {
  758. $this->error('车架号最多50字');
  759. break;
  760. }
  761. if ($v['images']) {
  762. $image_arr = explode(',', $v['images']);
  763. if (count($image_arr) > 9) {
  764. $this->error('每辆车照片最多9张');
  765. break;
  766. }
  767. $data['images'] = $v['images'];
  768. }
  769. $data['user_id'] = $this->auth->id;
  770. $data['carnumber'] = $v['carnumber'];
  771. $data['carframenumber'] = $v['carframenumber'];
  772. $data['createtime'] = time();
  773. array_push($_data, $data);
  774. }
  775. $rs = Db::name('user_pig_car')->insertAll($_data);
  776. if (!$rs) {
  777. $this->error('添加失败');
  778. }
  779. $this->success('添加成功');
  780. }
  781. //删除猪车信息
  782. public function delpigcar()
  783. {
  784. $id = input('id', 0, 'intval'); //猪车id
  785. if (!$id) {
  786. $this->error('请选择要删除的猪车');
  787. }
  788. $info = Db::name('user_pig_car')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  789. if (!$info) {
  790. $this->error('这不是您的车');
  791. }
  792. $rs = Db::name('user_pig_car')->where(['id' => $id, 'user_id' => $this->auth->id])->delete();
  793. if (!$rs) {
  794. $this->error('删除失败');
  795. }
  796. $this->success('删除成功');
  797. }
  798. //平台介绍
  799. public function getabout()
  800. {
  801. $info = Db::name('platform_info')->field('title, content')->where(['type' => 1])->find();
  802. $this->success('平台介绍', $info);
  803. }
  804. //常见问题
  805. public function problem()
  806. {
  807. $list = Db::name('problem')->field('id, title, content')
  808. ->page($this->page, config('paginate.list_rows'))->order('weigh', 'desc')->select();
  809. $this->success('常见问题', $list);
  810. }
  811. //投诉举报类型
  812. public function complainttype()
  813. {
  814. $list = Db::name('complaint_type')->field('id, name')->where(['pid' => 0])->order('weigh', 'desc')->select();
  815. $list = $this->getchildtype($list);
  816. $this->success('常见问题', $list);
  817. }
  818. //无限级投诉举报类型
  819. public function getchildtype($list = [])
  820. {
  821. $complaint_type = Db::name('complaint_type');
  822. foreach ($list as &$v) {
  823. $child = $complaint_type->field('id, name')->where(['pid' => $v['id']])->order('weigh', 'desc')->select();
  824. if ($child) {
  825. $child = $this->getchildtype($child);
  826. }
  827. $v['child'] = $child;
  828. }
  829. return $list;
  830. }
  831. //投诉举报
  832. public function complaint()
  833. {
  834. $complaint_type = input('complaint_type', '', 'trim'); //投诉类型
  835. $order_sn = input('order_sn', '', 'trim'); //订单号
  836. $be_complaint_user = input('be_complaint_user', '', 'trim'); //被投诉人/单位
  837. $complaint_user = input('complaint_user', '', 'trim'); //投诉人
  838. $mobile = input('mobile', '', 'trim'); //联系电话
  839. $code = input('code', '', 'trim'); //验证码
  840. $content = input('content', '', 'trim'); //情况说明
  841. $images = input('images', '', 'trim'); //附件材料
  842. $data = [];
  843. if (!$complaint_type) {
  844. $this->error('请选择投诉类型');
  845. }
  846. if ($order_sn) {
  847. if (iconv_strlen($order_sn, 'utf-8') > 255) {
  848. $this->error('订单号最多255位');
  849. }
  850. //查询订单
  851. $count = Db::name('mark_bid')->where(['user_id' => $this->auth->id, 'order_sn' => $order_sn, 'status' => 1])->count();
  852. if (!$count) {
  853. $this->error('请输入正确中标订单号');
  854. }
  855. }
  856. if (!$be_complaint_user) {
  857. $this->error('请输入被投诉人/单位');
  858. }
  859. if (iconv_strlen($be_complaint_user, 'utf-8') > 50) {
  860. $this->error('被投诉人/单位最多50字');
  861. }
  862. if (!$complaint_user) {
  863. $this->error('请输入投诉人');
  864. }
  865. if (iconv_strlen($complaint_user, 'utf-8') > 50) {
  866. $this->error('投诉人最多50字');
  867. }
  868. if (!is_mobile($mobile)) {
  869. $this->error('请输入正确联系电话');
  870. }
  871. if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  872. $this->error(__('Captcha is incorrect'));
  873. }
  874. $ret = Sms::check($mobile, $code, 'complaint');
  875. if (!$ret) {
  876. $this->error(__('Captcha is incorrect'));
  877. }
  878. if (!$content) {
  879. $this->error('请输入情况说明');
  880. }
  881. if (iconv_strlen($content, 'utf-8') > 3000) {
  882. $this->error('情况说明最多3000字');
  883. }
  884. if ($images) {
  885. $image_arr = explode(',', $images);
  886. if (count($image_arr) > 9) {
  887. $this->error('附件照片最多9张');
  888. }
  889. $data['images'] = $images;
  890. }
  891. $data['user_id'] = $this->auth->id;
  892. $data['complaint_type'] = $complaint_type;
  893. $data['order_sn'] = $order_sn;
  894. $data['be_complaint_user'] = $be_complaint_user;
  895. $data['complaint_user'] = $complaint_user;
  896. $data['mobile'] = $mobile;
  897. $data['content'] = $content;
  898. $data['images'] = $images;
  899. $data['createtime'] = time();
  900. $rs = Db::name('complaint')->insertGetId($data);
  901. if (!$rs) {
  902. $this->error('投诉失败');
  903. }
  904. Sms::flush($this->auth->mobile, 'complaint');
  905. $this->success('投诉成功');
  906. }
  907. //评价列表
  908. public function getmarkcomment()
  909. {
  910. $list = Db::name('mark_bid_comment')->where(['user_id' => $this->auth->id])
  911. ->page($this->page, config('paginate.list_rows'))->order('createtime', 'desc')->select();
  912. $list = list_domain_image($list, ['images']);
  913. $mark = Db::name('mark');
  914. $mark_bid = Db::name('mark_bid');
  915. foreach ($list as &$v) {
  916. $v['mark'] = $mark->find($v['mark_id']);
  917. $v['mark'] = info_domain_image($v['mark'], ['image', 'video', 'defective_images', 'road_images', 'pig_out_images']);
  918. $v['order_sn'] = $mark_bid->where(['id' => $v['mark_bid_id']])->value('order_sn');
  919. }
  920. $this->success('评论列表', $list);
  921. }
  922. //评价
  923. public function markcomment()
  924. {
  925. $mark_id = input('mark_id', 0, 'intval'); //招标id
  926. $content = input('content', '', 'trim'); //情况说明
  927. $images = input('images', '', 'trim'); //附件材料
  928. $data = [];
  929. if (!$mark_id) {
  930. $this->error('请选择要评价的数据');
  931. }
  932. $info = Db::name('mark')->find($mark_id);
  933. if (!$info) {
  934. $this->error('招标信息不存在');
  935. }
  936. $bid_id = Db::name('mark_bid')->where(['user_id' => $this->auth->id, 'mark_id' => $mark_id, 'status' => 1])->value('id');
  937. if (!$bid_id) {
  938. $this->error('您未中标,暂不能评价');
  939. }
  940. if (!$content) {
  941. $this->error('请输入评价内容');
  942. }
  943. if (iconv_strlen($content, 'utf-8') > 3000) {
  944. $this->error('评价内容最多3000字');
  945. }
  946. if ($images) {
  947. $image_arr = explode(',', $images);
  948. if (count($image_arr) > 9) {
  949. $this->error('照片最多9张');
  950. }
  951. $data['images'] = $images;
  952. }
  953. $data['user_id'] = $this->auth->id;
  954. $data['mark_id'] = $mark_id;
  955. $data['mark_bid_id'] = $bid_id;
  956. $data['content'] = $content;
  957. $data['images'] = $images;
  958. $data['createtime'] = time();
  959. $rs = Db::name('mark_bid_comment')->insertGetId($data);
  960. if (!$rs) {
  961. $this->error('评价失败');
  962. }
  963. $this->success('评价成功');
  964. }
  965. //个人账单列表
  966. public function getcompanymoney()
  967. {
  968. $list = Db::name('company')->field('id, name')->page($this->page, config('paginate.list_rows'))->select();
  969. $company_money_log = Db::name('company_money_log');
  970. foreach ($list as &$v) {
  971. $log = $company_money_log->where(['company_id' => $v['id'], 'user_id' => $this->auth->id])->order('id', 'desc')->find();
  972. if ($log) {
  973. $v['money'] = $log['after'];
  974. $v['can_money'] = $log['after'];
  975. } else {
  976. $v['money'] = '0';
  977. $v['can_money'] = '0';
  978. }
  979. }
  980. $this->success('个人账单列表', $list);
  981. }
  982. //个人账单明细
  983. public function getcompanymoneydetail()
  984. {
  985. $company_id = input('company_id', 0, 'intval'); //公司id
  986. $start_time = input('start_time', 0, 'strtotime'); //开始时间
  987. $end_time = input('end_time', 0, 'strtotime'); //结束时间
  988. if (!$company_id) {
  989. $this->error('参数缺失');
  990. }
  991. $where['company_id'] = $company_id;
  992. $where['user_id'] = $this->auth->id;
  993. if ($start_time && $end_time) {
  994. $where['createtime'] = ['between', [$start_time, $end_time]];
  995. }
  996. $list = Db::name('company_money_log')->field('id, money, memo, createtime')
  997. ->where($where)->page($this->page, config('paginate.list_rows'))->order('id', 'desc')->select();
  998. foreach ($list as &$v) {
  999. $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
  1000. }
  1001. $data['income'] = Db::name('company_money_log')->where($where)->where(['money' => ['gt', 0]])->sum('money');
  1002. $data['expend'] = Db::name('company_money_log')->where($where)->where(['money' => ['lt', 0]])->sum('money');
  1003. $data['list'] = $list;
  1004. $this->success('个人账单明细', $data);
  1005. }
  1006. //关于我们/免责协议/用户协议/隐私政策
  1007. public function getagreement()
  1008. {
  1009. $type = input('type', 0, 'intval');
  1010. if (!in_array($type, [1, 2, 3, 4])) {
  1011. $this->error('参数错误');
  1012. }
  1013. $info = Db::name('platform_info')->field('title, content')->where(['type' => $type])->find();
  1014. $this->success('协议', $info);
  1015. }
  1016. //获取openid
  1017. public function getopenid() {
  1018. //code
  1019. $code = $this->request->param('code', '', 'trim');// code值
  1020. if (!$code) {
  1021. $this->error(__('Invalid parameters'));
  1022. }
  1023. $config = config('wxchatpay');
  1024. $getopenid_url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['app_id'].'&secret='.$config['app_secret'].'&js_code='.$code.'&grant_type=authorization_code';
  1025. $openidInfo = httpRequest($getopenid_url, 'GET');//$this->getJson($getopenid_url);
  1026. $openidInfo = json_decode($openidInfo,true);
  1027. if(!isset($openidInfo['openid'])) {
  1028. $this->error('用户openid获取失败', $openidInfo);
  1029. }
  1030. $this->success('success', $openidInfo);
  1031. }
  1032. //微信登录
  1033. public function wxlogin() {
  1034. $openid = input('openid', '', 'trim');
  1035. if (!$openid) {
  1036. $this->error('参数缺失');
  1037. }
  1038. $user = \app\common\model\User::getByOpenid($openid);
  1039. if (!$user) {
  1040. $this->success('用户尚未注册', ['code' => 5]);
  1041. }
  1042. if ($user['status'] != 1) {
  1043. $this->error(__('Account is locked'));
  1044. }
  1045. $ret = $this->auth->direct($user->id);
  1046. if ($ret) {
  1047. $data = ['userinfo' => $this->auth->getUserinfo()];
  1048. $this->success(__('Logged in successful'), $data);
  1049. } else {
  1050. $this->error($this->auth->getError());
  1051. }
  1052. }
  1053. //注册
  1054. public function register()
  1055. {
  1056. $mobile = $this->request->post('mobile', '', 'trim'); //手机号
  1057. $code = $this->request->post('code', '', 'trim'); //验证码
  1058. $birthday = $this->request->post('birthday', '', 'strtotime'); //生日
  1059. $gender = $this->request->post('gender', 0, 'intval'); //性别:1=男,2=女
  1060. $invite_no = $this->request->post('invite_no', '', 'trim'); //邀请码
  1061. $openid = $this->request->post('openid', '', 'trim'); //微信openid
  1062. $nickname = $this->request->post('nickname', '', 'trim'); //微信昵称
  1063. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars'); //微信头像
  1064. $openidcount = Db::name('user')->where(['openid' => $openid])->count('id');
  1065. if ($openidcount) {
  1066. $this->error('微信已经注册,请直接登录');
  1067. }
  1068. if (!Validate::regex($mobile, "^1\d{10}$")) {
  1069. $this->error(__('Mobile is incorrect'));
  1070. }
  1071. $count = Db::name('user')->where(['mobile' => $mobile])->count('id');
  1072. if ($count) {
  1073. $this->error('手机号已被注册');
  1074. }
  1075. if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
  1076. $this->error(__('Captcha is incorrect'));
  1077. }
  1078. $ret = Sms::check($mobile, $code, 'register');
  1079. if (!$ret) {
  1080. $this->error(__('Captcha is incorrect'));
  1081. }
  1082. if (!$birthday || $birthday >= time()) {
  1083. $this->error('请选择正确生日');
  1084. }
  1085. if (!in_array($gender, [1, 2])) {
  1086. $this->error('请选择性别');
  1087. }
  1088. if ($invite_no) {
  1089. $invite_info = Db::name('user')->where(['invite_no' => $invite_no])->find();
  1090. if (!$invite_info) {
  1091. $this->error('邀请码不存在');
  1092. }
  1093. }
  1094. if (!$nickname || !$avatar) {
  1095. $this->error('参数缺失');
  1096. }
  1097. if (iconv_strlen($nickname, 'utf-8') > 30 || iconv_strlen($avatar, 'utf-8') > 255) {
  1098. $this->error('参数错误');
  1099. }
  1100. $ip = request()->ip();
  1101. $time = time();
  1102. $data = [
  1103. 'nickname' => $nickname,
  1104. 'salt' => Random::alnum(),
  1105. 'mobile' => $mobile,
  1106. 'avatar' => $avatar,
  1107. 'joinip' => $ip,
  1108. 'jointime' => $time,
  1109. 'logintime' => $time,
  1110. 'loginip' => $ip,
  1111. 'prevtime' => $time,
  1112. 'createtime' => $time,
  1113. 'gender' => $gender,
  1114. 'birthday' => $birthday,
  1115. 'openid' => $openid,
  1116. 'invite_no' => $this->myinvite(),
  1117. 'pre_user_id' => isset($invite_info) ? $invite_info['id'] : 0,
  1118. 'invite_time' => $time,
  1119. ];
  1120. //开启事务
  1121. Db::startTrans();
  1122. $rs = Db::name('user')->insertGetId($data);
  1123. if (!$rs) {
  1124. Db::rollback();
  1125. $this->error('注册失败');
  1126. }
  1127. //生成uid
  1128. $username = $this->myuid($rs);
  1129. $rt = Db::name('user')->where(['id' => $rs])->setField('username', $username);
  1130. if (!$rt) {
  1131. Db::rollback();
  1132. $this->error('注册失败');
  1133. }
  1134. //给用户发放注册优惠券
  1135. $register_coupon = Db::name('coupon')->where(['purpose' => 1, 'status' => 1])->order('weigh desc, id desc')->find();
  1136. if ($register_coupon) {
  1137. $register_coupon_data = [
  1138. 'user_id' => $rs,
  1139. 'coupon_id' => $register_coupon['id'],
  1140. 'title' => $register_coupon['title'],
  1141. 'desc' => $register_coupon['desc'],
  1142. 'type' => $register_coupon['type'],
  1143. 'money' => $register_coupon['money'],
  1144. 'minmoney' => $register_coupon['minmoney'],
  1145. 'purpose' => $register_coupon['purpose'],
  1146. 'starttime' => time(),
  1147. 'endtime' => time() + $register_coupon['effectiveday'] * 86400,
  1148. 'createtime' => time()
  1149. ];
  1150. $register_coupon_rs = Db::name('user_coupon')->insertGetId($register_coupon_data);
  1151. if (!$register_coupon_rs) {
  1152. Db::rollback();
  1153. $this->error('注册失败');
  1154. }
  1155. }
  1156. //查询是否需要给上级发放优惠券
  1157. if (isset($invite_info)) {
  1158. //查询上一次获得推广优惠券时间
  1159. $last_coupon = Db::name('user_coupon')->where(['user_id' => $invite_info['id'], 'purpose' => 2])->order('id', 'desc')->find();
  1160. $last_coupon_time = $last_coupon ? $last_coupon['createtime'] : 0;
  1161. //获取后台配置推广获得优惠券指定人数
  1162. $invitepersonnum = (int)config('site.invitepersonnum');
  1163. //查询最新推广任务
  1164. $invite_count = Db::name('user')->where(['pre_user_id' => $invite_info['id'], 'createtime' => ['gt', $last_coupon_time]])->count();
  1165. if ($invite_count >= $invitepersonnum && $invitepersonnum > 0) {
  1166. //每邀请10人发放一次优惠券
  1167. //查询推广优惠券
  1168. $invite_coupon = Db::name('coupon')->where(['purpose' => 2, 'status' => 1])->order('weigh desc, id desc')->find();
  1169. if ($invite_coupon) {
  1170. $invite_coupon_data = [
  1171. 'user_id' => $invite_info['id'],
  1172. 'coupon_id' => $invite_coupon['id'],
  1173. 'title' => $invite_coupon['title'],
  1174. 'desc' => $invite_coupon['desc'],
  1175. 'type' => $invite_coupon['type'],
  1176. 'money' => $invite_coupon['money'],
  1177. 'minmoney' => $invite_coupon['minmoney'],
  1178. 'purpose' => $invite_coupon['purpose'],
  1179. 'starttime' => time(),
  1180. 'endtime' => time() + $invite_coupon['effectiveday'] * 86400,
  1181. 'createtime' => time()
  1182. ];
  1183. $invite_coupon_rs = Db::name('user_coupon')->insertGetId($invite_coupon_data);
  1184. if (!$invite_coupon_rs) {
  1185. Db::rollback();
  1186. $this->error('注册失败');
  1187. }
  1188. }
  1189. }
  1190. }
  1191. Db::commit();
  1192. $ret = $this->auth->direct($rs);
  1193. if ($ret) {
  1194. $data = ['userinfo' => $this->auth->getUserinfo()];
  1195. $this->success(__('Sign up successful'), $data);
  1196. } else {
  1197. $this->error($this->auth->getError());
  1198. }
  1199. }
  1200. //生成邀请码
  1201. public function myinvite() {
  1202. $invite = Random::alnum(7);
  1203. $count = Db::name('user')->where(['invite_no' => $invite])->count('id');
  1204. if ($count) {
  1205. $this->myinvite();
  1206. }
  1207. return $invite;
  1208. }
  1209. //生成uid
  1210. public function myuid($id = 0) {
  1211. if (strlen($id) < 8) {
  1212. $username_len = 8 - strlen($id);
  1213. $username = $id . Random::numeric($username_len);
  1214. } else {
  1215. $username = $id;
  1216. }
  1217. $count = Db::name('user')->where(['username' => $username])->count('id');
  1218. if ($count) {
  1219. $this->myuid($id);
  1220. }
  1221. return $username;
  1222. }
  1223. //查询是否有未读消息
  1224. public function getreadsysmsg() {
  1225. $count = Db::name('sys_msg')->where(['user_id' => $this->auth->id, 'is_read' => 0])->count('id');
  1226. $this->success('查询是否有未读消息', $count);
  1227. }
  1228. //消息列表
  1229. public function sysmsg() {
  1230. $list = Db::name('sys_msg')->field('id, title, content, createtime')->where(['user_id' => $this->auth->id])
  1231. ->page($this->page, $this->pagenum)->order('id desc')->select();
  1232. foreach ($list as &$v) {
  1233. $v['createtime'] = date('Y-m-d H:i', $v['createtime']);
  1234. }
  1235. //更改消息状态
  1236. Db::name('sys_msg')->where(['user_id' => $this->auth->id, 'is_read' => 0])->setField('is_read', 1);
  1237. $this->success('消息列表', $list);
  1238. }
  1239. //查询当前会员等级信息
  1240. public function uservip() {
  1241. //更新会员等级
  1242. $this->checkviplevel($this->auth->id);
  1243. //用户信息
  1244. $user = Db::name('user')->find($this->auth->id);
  1245. //会员信息
  1246. $vip_info = Db::name('vip')->find($user['maxlevel']);
  1247. //下一级所需成长值
  1248. $next_vip = Db::name('vip')->where(['id' => ['gt', $user['maxlevel']]])->find();
  1249. if (!$next_vip) {
  1250. //顶部信息
  1251. $vip_info['vip_desc'] = '已是最高会员';
  1252. //成长值百分比0-100
  1253. $vip_info['growthvalue_percentage'] = 100;
  1254. } else {
  1255. //顶部信息
  1256. $need_growthvalue = $next_vip['growthvalue'] - $user['growthvalue'];
  1257. $vip_info['vip_desc'] = $vip_info['title'] . '还需' . $need_growthvalue . '成长值成为' . $next_vip['title'];
  1258. //成长值百分比0-100
  1259. $vip_info['growthvalue_percentage'] = ceil($user['growthvalue'] / $next_vip['growthvalue'] * 100);
  1260. }
  1261. //体验会员到期时间
  1262. $vip_info['experiencetime'] = $user['experiencetime'] > time() ? date('Y-m-d', $user['experiencetime']) : '';
  1263. //判断是不是生日 1是 0否
  1264. $vip_info['is_birth'] = date('md', time()) == date('md', strtotime($this->auth->birthday)) ? 1: 0;
  1265. $this->success('查询当前会员等级信息', $vip_info);
  1266. }
  1267. //查询所有会员
  1268. public function vip() {
  1269. //更新会员等级
  1270. $this->checkviplevel($this->auth->id);
  1271. //用户信息
  1272. $user = Db::name('user')->find($this->auth->id);
  1273. //会员信息
  1274. $vip_info = Db::name('vip')->find($user['growthlevel']);
  1275. //下一级所需成长值
  1276. $next_vip = Db::name('vip')->where(['id' => ['gt', $user['growthlevel']]])->find();
  1277. if (!$next_vip) {
  1278. //顶部信息
  1279. $vip_info['vip_desc'] = '已是最高会员';
  1280. //成长值百分比0-100
  1281. $vip_info['growthvalue_percentage'] = 100;
  1282. } else {
  1283. //顶部信息
  1284. $need_growthvalue = $next_vip['growthvalue'] - $user['growthvalue'];
  1285. $vip_info['vip_desc'] = $vip_info['title'] . '还需' . $need_growthvalue . '成长值成为' . $next_vip['title'];
  1286. //成长值百分比0-100
  1287. $vip_info['growthvalue_percentage'] = ceil($user['growthvalue'] / $next_vip['growthvalue'] * 100);
  1288. }
  1289. //所有会员列表
  1290. $list = Db::name('vip')->select();
  1291. $vip_privilege = Db::name('vip_privilege');
  1292. foreach ($list as $k => &$v) {
  1293. $v['vip_desc'] = ''; //顶部信息
  1294. $v['growthvalue_percentage'] = 0; //成长值百分比0-100
  1295. $v['experiencetime'] = ''; //体验会员到期时间
  1296. if ($v['id'] == $user['maxlevel']) {
  1297. $v['now_level'] = 1; //是否是当前等级 1是 0否
  1298. if ($user['maxlevel'] == $user['experiencelevel'] && $user['experiencetime'] > time()) {
  1299. //实际有效会员ID是体验会员id时, 展示体验会员到期时间
  1300. $v['experiencetime'] = '有效期至' . date('Y-m-d', $user['experiencetime']);
  1301. }
  1302. } else {
  1303. $v['now_level'] = 0;
  1304. }
  1305. if ($v['id'] == $user['growthlevel']) {
  1306. $v = array_merge($v, $vip_info);
  1307. }
  1308. $v['vip_privilege'] = $vip_privilege->field('id, title, desc')
  1309. ->where(['vip_id' => $v['id']])->order('weigh desc')->select();
  1310. }
  1311. $this->success('查询所有会员', $list);
  1312. }
  1313. //意见反馈
  1314. public function feedback() {
  1315. $content = input('content', '', 'trim'); //问题描述
  1316. $images = input('images', '', 'trim'); //反馈图片,最多9张用,拼接
  1317. $mobile = input('mobile', '', 'trim'); //联系电话
  1318. $data = [];
  1319. if (!$content) {
  1320. $this->error('请输入情况说明');
  1321. }
  1322. if (iconv_strlen($content, 'utf-8') > 3000) {
  1323. $this->error('情况说明最多3000字');
  1324. }
  1325. if ($images) {
  1326. $image_arr = explode(',', $images);
  1327. if (count($image_arr) > 9) {
  1328. $this->error('附件照片最多9张');
  1329. }
  1330. $data['images'] = $images;
  1331. }
  1332. if (!is_mobile($mobile)) {
  1333. $this->error('请输入正确联系电话');
  1334. }
  1335. $data['user_id'] = $this->auth->id;
  1336. $data['content'] = $content;
  1337. $data['images'] = $images;
  1338. $data['mobile'] = $mobile;
  1339. $data['createtime'] = time();
  1340. $rs = Db::name('feedback')->insertGetId($data);
  1341. if (!$rs) {
  1342. $this->error('反馈失败');
  1343. }
  1344. $this->success('反馈成功');
  1345. }
  1346. //我的优惠券
  1347. public function mycoupon() {
  1348. $type = input('type', 0, 'intval'); //类型:1未使用 2已使用 3已过期
  1349. if (!in_array($type, [1, 2, 3])) {
  1350. $this->error('参数错误');
  1351. }
  1352. $where['user_id'] = $this->auth->id;
  1353. if ($type == 1) {
  1354. $where['endtime'] = ['egt', time()];
  1355. $where['status'] = 0;
  1356. } elseif ($type == 2) {
  1357. $where['status'] = 1;
  1358. } elseif ($type == 3) {
  1359. $where['endtime'] = ['lt', time()];
  1360. $where['status'] = 0;
  1361. }
  1362. $list = Db::name('user_coupon')->field('id, title, desc, type, money, minmoney, endtime')->where($where)->page($this->page, $this->pagenum)->select();
  1363. foreach ($list as &$v) {
  1364. $v['endtime'] = date('Y-m-d', $v['endtime']);
  1365. }
  1366. $this->success('我的优惠券', $list);
  1367. }
  1368. //充值列表
  1369. public function rechargelist() {
  1370. $list = Db::name('recharge')->field('id, title, price, coupon_id, couponnum')->order('weigh desc')->select();
  1371. $coupon = Db::name('coupon');
  1372. foreach ($list as &$v) {
  1373. $coupon_info = $coupon->find($v['coupon_id']);
  1374. if ($coupon_info) {
  1375. if ($coupon_info['type'] == 1) {
  1376. $v['coupon_title'] = $coupon_info['money'] . '折';
  1377. } else {
  1378. $v['coupon_title'] = $coupon_info['money'] . '元';
  1379. }
  1380. } else {
  1381. $v['coupon_title'] = '';
  1382. }
  1383. }
  1384. $this->success('充值列表', $list);
  1385. }
  1386. //资金明细
  1387. public function usermoneylog() {
  1388. $list = Db::name('user_money_log')->field('id, money, memo, createtime')
  1389. ->where(['user_id' => $this->auth->id])
  1390. ->page($this->page, $this->pagenum)
  1391. ->order('id desc')->select();
  1392. foreach ($list as &$v) {
  1393. $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
  1394. }
  1395. $this->success('资金明细', $list);
  1396. }
  1397. //充值
  1398. public function recharge() {
  1399. $id = input('id', 0, 'intval');
  1400. if (!$id) {
  1401. $this->error('参数缺失');
  1402. }
  1403. $info = Db::name('recharge')->where(['id' => $id])->find();
  1404. if (!$info) {
  1405. $this->error('充值金额不存在');
  1406. }
  1407. if ($info['price'] <= 0) {
  1408. $this->error('充值价格异常');
  1409. }
  1410. //生成支付订单记录
  1411. $rechar_order['user_id'] = $this->auth->id;
  1412. $rechar_order['order_no'] = date('YmdHis', time()) . rand(10000000, 99999999); //微信订单编号
  1413. $rechar_order['money'] = $info['price'];
  1414. $rechar_order['purpose'] = 2; //充值用途:1=支付订单,2=充值,3=开通会员
  1415. $rechar_order['pay_type'] = 'wechat';
  1416. $rechar_order['relation_id'] = $id;
  1417. $rechar_order['createtime'] = time();
  1418. //开始事务
  1419. $result = Db::name('rechar_order')->insertGetId($rechar_order);
  1420. if (!$result) {
  1421. $this->error('网络延迟,请稍后再试');
  1422. }
  1423. //构建支付链接数据
  1424. $wxData['body'] = '充值';
  1425. $wxData['out_trade_no'] = $rechar_order['order_no'];
  1426. $wxData['total_fee'] = $info['price'];
  1427. // $wxData['total_fee'] = 0.01;
  1428. $wxData['openid'] = $this->auth->openid;
  1429. // require_once($_SERVER['DOCUMENT_ROOT'] . '/Plugins/Weixin/WxPay/WxPay.php');
  1430. $wxPay = new wxpay\WxPay(config('wxchatpay'));
  1431. $doResult = $wxPay->WxPayJs($wxData);
  1432. $this->success('微信支付参数返回成功', $doResult);
  1433. }
  1434. //邀请好友顶部信息
  1435. public function invitetop() {
  1436. $data['invite_no'] = $this->auth->invite_no; //邀请码
  1437. $data['invite_count'] = Db::name('user')->where(['pre_user_id' => $this->auth->id])->count('id'); //邀请人数
  1438. $data['coupon_count'] = Db::name('user_coupon')->where(['user_id' => $this->auth->id, 'purpose' => ['in', [2, 5]]])->count('id');
  1439. $this->success('邀请好友顶部信息', $data);
  1440. }
  1441. //邀请好友底部列表
  1442. public function invitebottom() {
  1443. $type = input('type', 0, 'intval'); //1邀请的好友 2报名的好友
  1444. if (!in_array($type, [1, 2])) {
  1445. $this->error('参数错误');
  1446. }
  1447. if ($type == 1) {
  1448. $list = Db::name('user')->field('id, avatar, nickname, invite_time')
  1449. ->where(['pre_user_id' => $this->auth->id])->page($this->page, $this->pagenum)->order('id desc')->select();
  1450. } else {
  1451. $invite_ids = Db::name('user')->where(['pre_user_id' => $this->auth->id])->column('id');
  1452. if (!$invite_ids) {
  1453. $this->success('报名好友列表', []);
  1454. }
  1455. $list = Db::name('active_order')->alias('a')
  1456. ->join('hu_user b', 'a.user_id = b.id', 'left')
  1457. ->field('b.id, b.avatar, b.nickname, b.invite_time')
  1458. ->where(['a.user_id' => ['in', $invite_ids]])
  1459. ->page($this->page, $this->pagenum)
  1460. ->order('a.id desc')->select();
  1461. }
  1462. $list = list_domain_image($list, ['avatar']);
  1463. foreach ($list as &$v) {
  1464. $v['invite_time'] = date('Y-m-d', $v['invite_time']);
  1465. }
  1466. $this->success('邀请好友底部列表', $list);
  1467. }
  1468. //我的订单
  1469. public function myorder() {
  1470. $type = input('type', 0, 'intval'); //状态:0=待付款,1=待出行,2=已完成,3=已取消
  1471. if (!in_array($type, [0, 1, 2, 3])) {
  1472. $this->error('参数错误');
  1473. }
  1474. $list = Db::name('active_order')->where(['user_id' => $this->auth->id, 'status' => $type])->page($this->page, $this->pagenum)->order('id desc')->select();
  1475. $active = Db::name('active');
  1476. foreach ($list as &$v) {
  1477. $v['button_status'] = 0;//待出行按钮: 0不展示 1退款 2修改联系人
  1478. $v['active'] = $active->find($v['active_id']);
  1479. $v['active'] = info_domain_image($v['active'], ['image']);
  1480. if ($v['status'] == 1) {
  1481. if ($v['active']['refundendtime'] >= time()) {
  1482. $v['button_status'] = 1;
  1483. } elseif ($v['active']['refundendtime'] < time() && $v['active']['signupendtime'] >= time()) {
  1484. $v['button_status'] = 2;
  1485. }
  1486. }
  1487. if ($v['active']['maxperson'] <= $v['active']['currentperson']) {
  1488. $v['is_full'] = 1; //已报满
  1489. } else {
  1490. $v['is_full'] = 0; //未报满
  1491. }
  1492. }
  1493. $this->success('我的订单', $list);
  1494. }
  1495. //我的订单详情
  1496. public function myorderinfo() {
  1497. $id = input('id', 0, 'intval'); //订单id
  1498. if (!$id) {
  1499. $this->error('参数缺失');
  1500. }
  1501. $info = Db::name('active_order')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1502. if (!$info) {
  1503. $this->error('数据不存在');
  1504. }
  1505. $info['createtime'] = date('Y-m-d H:i:s', $info['createtime']);
  1506. //查询活动信息
  1507. $active = Db::name('active')->find($info['active_id']);
  1508. //待出行按钮: 0不展示 1退款 2修改联系人
  1509. $info['button_status'] = 0;
  1510. if ($info['status'] == 1) {
  1511. if ($active['refundendtime'] >= time()) {
  1512. $info['button_status'] = 1;
  1513. } elseif ($active['refundendtime'] < time() && $active['signupendtime'] >= time()) {
  1514. $info['button_status'] = 2;
  1515. }
  1516. }
  1517. $active = info_domain_image($active, ['image']);
  1518. $active['starttime'] = date('Y-m-d H:i', $active['starttime']);
  1519. $active['endtime'] = date('Y-m-d H:i', $active['endtime']);
  1520. $active['collectiontime'] = date('Y-m-d H:i', $active['collectiontime']);
  1521. $active['signupendtime'] = date('Y-m-d H:i', $active['signupendtime']);
  1522. $active['refundendtime'] = date('Y-m-d H:i', $active['refundendtime']);
  1523. //查询报名人员
  1524. $active_people = Db::name('active_people')->where(['order_id' => $id])->field('id, name, mobile')->select();
  1525. $info['active'] = $active;
  1526. $info['active_people'] = $active_people;
  1527. $this->success('我的订单详情', $info);
  1528. }
  1529. //查看报名人信息
  1530. public function activepeople() {
  1531. $id = input('id', 0, 'intval'); //报名人员id
  1532. if (!$id) {
  1533. $this->error('参数缺失');
  1534. }
  1535. $info = Db::name('active_people')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1536. if (!$info) {
  1537. $this->error('数据不存在');
  1538. }
  1539. $this->success('查看报名人信息', $info);
  1540. }
  1541. //修改报名人信息
  1542. public function modifyactivepeople() {
  1543. $id = input('id', 0, 'intval'); //报名人员id
  1544. $name = input('name', '', 'trim'); //姓名
  1545. $idcard = input('idcard', '', 'trim'); //身份证号
  1546. $mobile = input('mobile', '', 'trim'); //手机号
  1547. $emergencycontact = input('emergencycontact', '', 'trim'); //紧急联系人
  1548. $contactmobile = input('contactmobile', '', 'trim'); //紧急联系方式
  1549. if (!$id) {
  1550. $this->error('参数缺失');
  1551. }
  1552. $info = Db::name('active_people')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1553. if (!$info) {
  1554. $this->error('数据不存在');
  1555. }
  1556. if ($info['status'] == 2) {
  1557. $this->success('订单已经结束');
  1558. }
  1559. if ($info['status'] == 3) {
  1560. $this->success('订单已经取消');
  1561. }
  1562. if ($info['modifystatus'] == 1) {
  1563. $this->error('报名信息正在修改中,请等待后台审核');
  1564. }
  1565. //查询活动状态
  1566. $active = Db::name('active')->find($info['active_id']);
  1567. if (!$active) {
  1568. $this->success('活动不存在或已取消');
  1569. }
  1570. // if ($active['status'] == 1) {
  1571. // $this->error('活动已成行,不能修改信息');
  1572. // }
  1573. if (time() <= $active['refundendtime'] || time() > $active['signupendtime']) {
  1574. $this->error('当前时间段暂不可修改信息');
  1575. }
  1576. //查询是否申请过退款
  1577. $active_refund = Db::name('active_refund')->where(['order_id' => $info['active_id'], 'status' => 0])->count('id');
  1578. if ($active_refund) {
  1579. $this->error('您正在申请退款,暂不可修改信息');
  1580. }
  1581. //检查信息
  1582. if (!$name || iconv_strlen($name, 'utf-8') > 50) {
  1583. $this->error('请输入正确姓名');
  1584. }
  1585. if (iconv_strlen($idcard, 'utf-8') != 18) {
  1586. $this->error('请输入正确身份证号');
  1587. }
  1588. if (!is_mobile($mobile)) {
  1589. $this->error('请输入正确手机号');
  1590. }
  1591. if (!$emergencycontact || iconv_strlen($emergencycontact, 'utf-8') > 50) {
  1592. $this->error('请输入紧急联系人');
  1593. }
  1594. if (!is_mobile($contactmobile)) {
  1595. $this->error('请输入正确紧急联系人方式');
  1596. }
  1597. //判断是否报名过
  1598. $count = Db::name('active_people')->where(['active_id' => $info['active_id'], 'idcard' => $idcard, 'status' => ['neq', 3]])->count('id');
  1599. if ($count) {
  1600. $this->error('该信息已报名过活动');
  1601. }
  1602. $count2 = Db::name('active_people_modify')->where(['active_id' => $info['active_id'], 'idcard' => $idcard, 'status' => 0])->count('id');
  1603. if ($count2) {
  1604. $this->error('该信息已提交过修改,请等待审核');
  1605. }
  1606. //构建修改数据
  1607. $data = [
  1608. 'active_id' => $info['active_id'],
  1609. 'order_id' => $info['order_id'],
  1610. 'people_id' => $id,
  1611. 'user_id' => $info['user_id'],
  1612. 'name' => $name,
  1613. 'credtype' => $info['credtype'],
  1614. 'idcard' => $idcard,
  1615. 'mobile' => $mobile,
  1616. 'emergencycontact' => $emergencycontact,
  1617. 'contactmobile' => $contactmobile,
  1618. 'insurance' => $info['insurance'],
  1619. 'originalprice' => $info['originalprice'],
  1620. 'vipprice' => $info['vipprice'],
  1621. 'coupon_id' => $info['coupon_id'],
  1622. 'coupontype' => $info['coupontype'],
  1623. 'couponprice' => $info['couponprice'],
  1624. 'is_free' => $info['is_free'],
  1625. 'price' => $info['price'],
  1626. 'is_self' => $info['is_self'],
  1627. 'createtime' => time()
  1628. ];
  1629. //开启事务
  1630. Db::startTrans();
  1631. //添加记录
  1632. $rs = Db::name('active_people_modify')->insertGetId($data);
  1633. if (!$rs) {
  1634. Db::rollback();
  1635. $this->error('修改失败');
  1636. }
  1637. //修改原记录状态
  1638. $rt = Db::name('active_people')->where(['id' => $id, 'modifystatus' => 0])->setField('modifystatus', 1);
  1639. if (!$rt) {
  1640. Db::rollback();
  1641. $this->error('修改失败');
  1642. }
  1643. Db::commit();
  1644. $this->success('修改成功,请等待审核');
  1645. }
  1646. //取消订单
  1647. public function cancelorder() {
  1648. $id = input('id', 0, 'intval'); //订单id
  1649. if (!$id) {
  1650. $this->error('参数缺失');
  1651. }
  1652. $info = Db::name('active_order')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1653. if (!$info) {
  1654. $this->error('数据不存在');
  1655. }
  1656. if ($info['status'] != 0) {
  1657. $this->error('网络延迟,请刷新重试');
  1658. }
  1659. if ($info['createtime'] + 1800 < time()) {
  1660. $this->error('订单超时,系统将自动取消');
  1661. }
  1662. //查询活动是否存在
  1663. $active_info = Db::name('active')->find($info['active_id']);
  1664. if (!$active_info) {
  1665. $this->error('活动不存在');
  1666. }
  1667. //开启事务
  1668. Db::startTrans();
  1669. //修改订单信息
  1670. $rs = Db::name('active_order')->where(['id' => $id, 'status' => 0])->setField('status', 3);
  1671. if (!$rs) {
  1672. Db::rollback();
  1673. $this->error('网络延迟,请稍后再试');
  1674. }
  1675. //修改活动人员
  1676. $rt = Db::name('active_people')->where(['order_id' => $id, 'status' => 0])->setField(['status' => 3, 'modifystatus' => 0]);
  1677. if (!$rt) {
  1678. Db::rollback();
  1679. $this->error('网络延迟,请稍后再试');
  1680. }
  1681. //修改活动人员修改记录
  1682. $res = Db::name('active_people_modify')->where(['order_id' => $id, 'status' => 0])->setField('status', 2);
  1683. if ($res === false) {
  1684. Db::rollback();
  1685. $this->error('网络延迟,请稍后再试');
  1686. }
  1687. //减少活动已报名人数
  1688. $currentperson = $active_info['currentperson'] - $info['number'];
  1689. $active_rs = Db::name('active')->where(['id' => $info['active_id'], 'currentperson' => $active_info['currentperson']])->setField('currentperson', $currentperson);
  1690. if (!$active_rs) {
  1691. Db::rollback();
  1692. $this->error('网络延迟,请稍后再试');
  1693. }
  1694. Db::commit();
  1695. $this->success('修改成功');
  1696. }
  1697. //支付订单
  1698. public function payorder() {
  1699. $id = input('id', 0, 'intval'); //订单id
  1700. if (!$id) {
  1701. $this->error('参数缺失');
  1702. }
  1703. $info = Db::name('active_order')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1704. if (!$info) {
  1705. $this->error('数据不存在');
  1706. }
  1707. if ($info['status'] != 0) {
  1708. $this->error('当前订单状态不能支付');
  1709. }
  1710. if ($info['createtime'] + 1800 < time()) {
  1711. $this->error('订单支付超时,请重新报名');
  1712. }
  1713. //查询活动是否存在
  1714. $active_info = Db::name('active')->find($info['active_id']);
  1715. if (!$active_info) {
  1716. $this->error('活动不存在');
  1717. }
  1718. if ($active_info['status'] == 2) {
  1719. $this->error('活动已经结束');
  1720. }
  1721. if ($active_info['status'] == 3) {
  1722. $this->error('活动已取消');
  1723. }
  1724. $pay_count = Db::name('rechar_order')->where(['user_id' => $this->auth->id, 'purpose' => 1, 'pay_type' => 'wechat', 'relation_id' => $id, 'status' => 1])->count('id');
  1725. if ($pay_count) {
  1726. $this->error('您已成功支付过,请勿重复支付');
  1727. }
  1728. //生成支付订单记录
  1729. $rechar_order['user_id'] = $this->auth->id;
  1730. $rechar_order['order_no'] = date('YmdHis', time()) . rand(10000000, 99999999); //微信订单编号
  1731. $rechar_order['money'] = $info['price'];
  1732. $rechar_order['purpose'] = 1; //充值用途:1=支付订单,2=充值,3=开通会员
  1733. $rechar_order['pay_type'] = 'wechat';
  1734. $rechar_order['relation_id'] = $id;
  1735. $rechar_order['createtime'] = time();
  1736. $result = Db::name('rechar_order')->insertGetId($rechar_order);
  1737. if (!$result) {
  1738. $this->error('网络延迟,请稍后再试');
  1739. }
  1740. //构建支付链接数据
  1741. $wxData['body'] = '报名活动支付';
  1742. $wxData['out_trade_no'] = $rechar_order['order_no'];
  1743. $wxData['total_fee'] = $info['price'];
  1744. // $wxData['total_fee'] = 0.01;
  1745. $wxData['openid'] = $this->auth->openid;
  1746. // require_once($_SERVER['DOCUMENT_ROOT'] . '/Plugins/Weixin/WxPay/WxPay.php');
  1747. $wxPay = new wxpay\WxPay(config('wxchatpay'));
  1748. $doResult = $wxPay->WxPayJs($wxData);
  1749. $this->success('微信支付参数返回成功', $doResult);
  1750. }
  1751. //申请退款
  1752. public function applyrefund() {
  1753. $id = input('id', 0, 'intval'); //订单id
  1754. if (!$id) {
  1755. $this->error('参数缺失');
  1756. }
  1757. $info = Db::name('active_order')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  1758. if (!$info) {
  1759. $this->error('数据不存在');
  1760. }
  1761. if ($info['status'] != 1) {
  1762. $this->error('当前订单状态不能退款');
  1763. }
  1764. //查询是否已经申请过
  1765. $count = Db::name('active_refund')->where(['order_id' => $id, 'status' => 0])->count('id');
  1766. if ($count) {
  1767. $this->error('您已申请过退款,请等待后台审核');
  1768. }
  1769. //查询活动是否存在
  1770. $active_info = Db::name('active')->find($info['active_id']);
  1771. if (!$active_info) {
  1772. $this->error('活动不存在');
  1773. }
  1774. if (time() > $active_info['refundendtime']) {
  1775. $this->error('已超过退款截止时间,暂不能退款');
  1776. }
  1777. //用户取消订单每人扣费金额(元)
  1778. $cancelorder_price = config('site.cancelorder');
  1779. if ($cancelorder_price < 0) {
  1780. $this->error('退款手续费异常,请联系管理员');
  1781. }
  1782. //退款金额
  1783. $refundprice = number_format($info['price'] - $cancelorder_price * $info['number'], 2, '.', '');
  1784. $refundprice = $refundprice > 0 ? $refundprice : 0;
  1785. //构建申请数据
  1786. $data = [
  1787. 'active_id' => $info['active_id'],
  1788. 'order_id' => $id,
  1789. 'user_id' => $this->auth->id,
  1790. 'paytype' => $info['paytype'],
  1791. 'price' => $info['price'],
  1792. 'number' => $info['number'],
  1793. 'transaction_id' => $info['transaction_id'],
  1794. 'refund_no' => $info['paytype'] ? date('YmdHis', time()) . rand(10000000, 99999999) : '', //退款单号
  1795. 'refundprice' => $refundprice,
  1796. 'createtime' => time()
  1797. ];
  1798. $rs = Db::name('active_refund')->insertGetId($data);
  1799. if (!$rs) {
  1800. $this->error('申请退款失败');
  1801. }
  1802. $this->success('申请退款成功,请等待审核');
  1803. }
  1804. }