BrandController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: Powerless < wzxaini9@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace battery\portal\controller;
  12. use battery\portal\model\BatteryBaseModel;
  13. use battery\portal\model\PingZfPlatform;
  14. use battery\portal\model\SdPlatform;
  15. use think\Config;
  16. use think\Request;
  17. use think\Validate;
  18. class BrandController extends BatteryBaseController
  19. {
  20. function index(){
  21. $this->setTitleName('自助选车');
  22. return $this->fetch();
  23. }
  24. function searchBrand(){
  25. $sdPlatform=new SdPlatform();
  26. $info=$sdPlatform->searchBrand();
  27. if($info['status']){
  28. $this->success('请求成功','',$info['info']);
  29. }else{
  30. $this->error('获取失败');
  31. }
  32. }
  33. function searchSeries(){
  34. $validate = new Validate([
  35. 'name' => 'require',
  36. ]);
  37. $validate->message([
  38. 'name.require' => '品牌名称',
  39. ]);
  40. $data = $this->request->param();
  41. if (!$validate->check($data)) {
  42. $this->error($validate->getError());
  43. }
  44. $sdPlatform=new SdPlatform();
  45. $info=$sdPlatform->searchSeries(trim($data['name']));
  46. if($info['status']){
  47. $this->success('请求成功','',$info['info']);
  48. }else{
  49. $this->error('获取失败');
  50. }
  51. }
  52. function searchYear(){
  53. $validate = new Validate([
  54. 'name' => 'require',
  55. 'type' => 'require',
  56. ]);
  57. $validate->message([
  58. 'name.require' => '品牌名称',
  59. 'type.require' => '车辆类型',
  60. ]);
  61. $data = $this->request->param();
  62. if (!$validate->check($data)) {
  63. $this->error($validate->getError());
  64. }
  65. $sdPlatform=new SdPlatform();
  66. $info=$sdPlatform->searchYear(trim($data['name']),trim($data['type']));
  67. if($info['status']){
  68. $this->success('请求成功','',$info['info']);
  69. }else{
  70. $this->error('获取失败');
  71. }
  72. }
  73. function searchModel(){
  74. $validate = new Validate([
  75. 'name' => 'require',
  76. 'type' => 'require',
  77. 'year' => 'require',
  78. ]);
  79. $validate->message([
  80. 'name.require' => '品牌名称',
  81. 'type.require' => '车辆类型',
  82. 'year.require' => '车辆类型',
  83. ]);
  84. $data = $this->request->param();
  85. if (!$validate->check($data)) {
  86. $this->error($validate->getError());
  87. }
  88. $sdPlatform=new SdPlatform();
  89. $info=$sdPlatform->searchModel(trim($data['name']),trim($data['type']),trim($data['year']));
  90. if($info['status']){
  91. $this->success('请求成功','',$info['info']);
  92. }else{
  93. $this->error('获取失败');
  94. }
  95. }
  96. function goods(){
  97. $validate = new Validate([
  98. 'name' => 'require',
  99. 'type' => 'require',
  100. 'year' => 'require',
  101. 'model' => 'require',
  102. 'carid' => 'require',
  103. ]);
  104. $validate->message([
  105. 'name.require' => '品牌名称',
  106. 'type.require' => '车辆类型',
  107. 'year.require' => '车辆类型',
  108. 'model.require' => '车辆类型',
  109. 'carid.require' => '车辆类型',
  110. ]);
  111. $data = $this->request->param();
  112. if (!$validate->check($data)) {
  113. $this->error($validate->getError());
  114. }
  115. $sdPlatform=new SdPlatform();
  116. $info=$sdPlatform->weblcMatch(trim($data['carid']));
  117. $sdata=array();
  118. if($info['status']){
  119. $sdata=$info['info'];
  120. }
  121. $this->setTitleName('电瓶上门安装');
  122. $this->assign('carname', trim($data['name']));
  123. $this->assign('cartype', trim($data['type']));
  124. $this->assign('caryear', trim($data['year']));
  125. $this->assign('carmodel', trim($data['model']));
  126. $this->assign('datalist', $sdata);
  127. return $this->fetch();
  128. }
  129. function getpay(){
  130. $validate = new Validate([
  131. 'name' => 'require',
  132. 'type' => 'require',
  133. 'year' => 'require',
  134. 'model' => 'require',
  135. 'gid' => 'require',
  136. ]);
  137. $validate->message([
  138. 'name.require' => '品牌名称',
  139. 'type.require' => '车辆类型',
  140. 'year.require' => '车辆年份',
  141. 'model.require' => '车辆型号',
  142. 'gid.require' => '商品ID',
  143. ]);
  144. $data = $this->request->param();
  145. if (!$validate->check($data)) {
  146. $this->error($validate->getError());
  147. }
  148. $batteryBaseModel = new BatteryBaseModel();
  149. $goodWhere['gid']=$data['gid'];
  150. $good=$batteryBaseModel->name('good')->where($goodWhere)->order('id DESC')->find();
  151. if(!$good){
  152. $this->error('获取商品失败');
  153. }
  154. $morderid = 'T' . $this->getOrderId();
  155. $time=time();
  156. $md5=md5($morderid.'rWY27jftsCVkawzmUtNi2b53niWbTopU');
  157. $orderData = array(
  158. 'order_id' => $morderid,
  159. 'gid' => intval($data['gid']),
  160. 'amount' => $good->price,
  161. 'create_time' => $time,
  162. 'up_time' => $time,
  163. 'brief' => '电池',
  164. 'car_name' => trim($data['name']),
  165. 'car_type' => trim($data['type']),
  166. 'car_year' => trim($data['year']),
  167. 'car_model' =>trim($data['model']),
  168. 'ip' => get_client_ip(),
  169. 'md5' => $md5
  170. );
  171. $tag=$batteryBaseModel->name('order')->insert($orderData);
  172. if($tag){
  173. $this->success('请求成功','',$md5);
  174. }else{
  175. $this->error('获取失败');
  176. }
  177. }
  178. function details(){
  179. $validate = new Validate([
  180. 'token' => 'require',
  181. ]);
  182. $validate->message([
  183. 'token.require' => '参数异常',
  184. ]);
  185. $data = $this->request->param();
  186. if (!$validate->check($data)) {
  187. $this->error($validate->getError());
  188. }
  189. $this->setTitleName('订单确认');
  190. $batteryBaseModel=new BatteryBaseModel();
  191. $where['md5']=$data['token'];
  192. $order=$batteryBaseModel->name('order')->where($where)->find();
  193. if($order){
  194. $order->amount=bcdiv($order->amount,100,2);
  195. $weekArr=array("日","一","二","三","四","五","六");
  196. $days=array();
  197. for($i=0;$i<35;$i++){
  198. $days[$i]['time']=date("m-d",strtotime('+'.$i.'day'));
  199. $days[$i]['time_date']=date("Y-m-d",strtotime('+'.$i.'day'));
  200. if ($i==0) {
  201. $days[$i]['week']= '今';
  202. $days[$i]['class']= 'jin';
  203. }else{
  204. $days[$i]['week']= $weekArr[date("w",strtotime('+'.$i.'day'))];
  205. }
  206. }
  207. $setdata=array();
  208. $str='';
  209. foreach ($days as $key=>$v){
  210. $str=$str."<td class=\"tb_item\" onclick=\"selecttime(this)\" data-time=\"{$v['time_date']}\">
  211. <div >{$v['time']}</div>
  212. <div class=\"tb_item_time\">{$v['week']}</div>
  213. </td>";
  214. if((($key+1)%7)==0){
  215. $html="<tr>".$str."</tr>";
  216. array_push($setdata,array('html'=>$html));
  217. $str='';
  218. }
  219. }
  220. $this->assign('order', $order);
  221. $this->assign('weekinfo', $setdata);
  222. $this->assign('token', trim($data['token']));
  223. return $this->fetch();
  224. }else{
  225. $this->error('订单信息不存在');
  226. }
  227. }
  228. //账单明细
  229. function payorder()
  230. {
  231. $validate = new Validate([
  232. 'token' => 'require',
  233. 'province' => 'require',
  234. 'city' => 'require',
  235. 'area' => 'require',
  236. 'user_name' => 'require',
  237. 'user_phone' => 'require',
  238. 'user_addr' => 'require',
  239. 'select_time' => 'require',
  240. ]);
  241. $validate->message([
  242. 'token.require' => '参数异常',
  243. 'province.require' => '用户姓名',
  244. 'city.require' => '用户姓名',
  245. 'area.require' => '用户姓名',
  246. 'user_name.require' => '用户姓名',
  247. 'user_phone.require' => '收款手机号',
  248. 'user_addr.require' => '用户地址',
  249. 'select_time.require' => '预约时间',
  250. ]);
  251. //TODO 手机校验没做
  252. $data = $this->request->param();
  253. if (!$validate->check($data)) {
  254. $this->error($validate->getError());
  255. }
  256. $select_time=strtotime($data['select_time']);
  257. if($select_time<=0||$select_time<time()){
  258. $this->error('预约时间有误');
  259. }
  260. $channel_id='alipay_wap';
  261. $where['md5']=$data['token'];
  262. $batteryBaseModel=new BatteryBaseModel();
  263. $batteryBaseModel->startTrans();
  264. $order=$batteryBaseModel->name('order')->where($where)->lock(true)->find();
  265. $time=time();
  266. if($order){
  267. $isNotAddr=false;
  268. $addrss_id=0;
  269. if($order->addrss_id===0){
  270. $isNotAddr=true;
  271. $addmd5=md5($data['province'].$data['city'].$data['area'].$data['user_name'].$data['user_phone'].$data['user_addr']);
  272. $addWhere['md5']=$addmd5;
  273. $addrss=$batteryBaseModel->name('addrss')->where($addWhere)->find();
  274. if(!$addrss){
  275. $dataAdd = array(
  276. 'name' => $data['user_name'],
  277. 'phone' => $data['user_phone'],
  278. 'province' => $data['province'],
  279. 'city' => $data['city'],
  280. 'area' => $data['area'],
  281. 'content' => $data['user_addr'],
  282. 'create_time' => $time,
  283. 'md5' => $addmd5
  284. );
  285. $addtag=$batteryBaseModel->name('addrss')->insert($dataAdd);
  286. if(!$addtag){
  287. $batteryBaseModel->rollback();
  288. $this->error('下单失败1004');
  289. }else{
  290. $addrss_id=$batteryBaseModel->getLastInsID();
  291. }
  292. }else{
  293. $addrss_id=$addrss->id;
  294. }
  295. }
  296. $paywhere['order_id']=$order->id;
  297. $payorder=$batteryBaseModel->name('pay_order')->where($paywhere)->lock(true)->find();
  298. if($payorder){
  299. $this->success('请求成功','',$payorder->pay_data);
  300. }else{
  301. if($isNotAddr){
  302. $orderUp['addrss_id']=$addrss_id;
  303. }
  304. $orderUp['select_time']=$select_time;
  305. $batteryBaseModel->name('order')->where($where)->update($orderUp);
  306. $dataPay = array(
  307. 'order_id' => $order->id,
  308. 'order_num' => $order->order_id,
  309. 'create_time' => $time,
  310. 'up_time' => $time,
  311. 'channel_id' => 1,
  312. // 'price' => $order->amount,
  313. 'price' => 1,
  314. 'md5' => md5( $order->id . $order->order_id . 'rWY27jftsCVkawzmUtNi2b53niWbTopU')
  315. );
  316. $tagPay = $batteryBaseModel->name('pay_order')->insert($dataPay);
  317. if($tagPay){
  318. $pingZfPlatform=new PingZfPlatform();
  319. $dataPay['success_url']=url('portal/brand','',true,true);
  320. $info=$pingZfPlatform->createCharge($dataPay,'电池上门服务','alipay_wap',get_client_ip(),'');
  321. if ($info) {
  322. $upDataPay['up_time'] = time();
  323. switch ($channel_id) {
  324. case 'alipay_wap':
  325. $upDataPay['pay_data'] = json_encode($info);
  326. break;
  327. }
  328. $upwhere['id'] = $batteryBaseModel->getLastInsID();
  329. $tagPay1 = $batteryBaseModel->name('pay_order')->where($upwhere)->update($upDataPay);
  330. if($tagPay1){
  331. $batteryBaseModel->commit();
  332. $this->success('请求成功','',$upDataPay['pay_data']);
  333. }else{
  334. $batteryBaseModel->rollback();
  335. $this->error('下单失败1002');
  336. }
  337. }else{
  338. $batteryBaseModel->rollback();
  339. $this->error('下单失败1003');
  340. }
  341. }else{
  342. $batteryBaseModel->rollback();
  343. $this->error('下单失败1001');
  344. }
  345. }
  346. }else{
  347. $batteryBaseModel->rollback();
  348. $this->error('查询订单失败');
  349. }
  350. }
  351. function submint(){
  352. $validate = new Validate([
  353. 'token' => 'require',
  354. ]);
  355. $validate->message([
  356. 'token.require' => '参数异常',
  357. ]);
  358. $data = $this->request->param();
  359. if (!$validate->check($data)) {
  360. $this->error($validate->getError());
  361. }
  362. $this->setTitleName('订单确认');
  363. $batteryBaseModel=new BatteryBaseModel();
  364. $where['md5']=$data['token'];
  365. $order=$batteryBaseModel->name('order')->where($where)->find();
  366. if($order){
  367. return $this->fetch();
  368. }else{
  369. $this->error('订单信息不存在');
  370. }
  371. }
  372. private function getOrderId($prefix = '300')
  373. {
  374. return $prefix . (strtotime(date('YmdHis', time()))) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  375. }
  376. }