User.php 72 KB

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