MakeModel.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <?php
  2. namespace Home\Model;
  3. use Think\Model;
  4. /**
  5. * 拼团模型模型
  6. * @author fish
  7. *
  8. */
  9. class MakeModel{
  10. /**
  11. * @author yj
  12. * @desc 获取码科accessToken
  13. * 结构:token + express_time
  14. * @param int $uniacid
  15. */
  16. public function getMakeAccessToken()
  17. {
  18. $token_json = S('_makeAccessToken');
  19. $token_data = json_decode( $token_json, true);
  20. $now_time = time();
  21. $is_get_new = false;
  22. if( empty($token_data) )
  23. {
  24. $is_get_new = true;
  25. }else {
  26. $ver_result = $this->verifyToken();
  27. if( $ver_result['code'] == 1 )
  28. {
  29. $is_get_new = true;
  30. }
  31. }
  32. if( $is_get_new )
  33. {
  34. $make_url = D('Home/Front')->get_config_by_name('localtown_mk_url');
  35. $wepro_appid = D('Home/Front')->get_config_by_name('wepro_appid');
  36. $token = D('Home/Front')->get_config_by_name('localtown_mk_token');
  37. //get_config_by_name('default_comunity_limit_mile');
  38. $url ="{$make_url}addons/make_speed/core/public/index.php/apis/v2/get_token";
  39. $post_data = array();
  40. $post_data['token'] = $token;
  41. $post_data['appid'] = $wepro_appid;
  42. $result_json = http_request($url , http_build_query($post_data) );
  43. $result = json_decode( $result_json , true );
  44. if( empty($result) || isset($result['error_code']) )
  45. {
  46. return ['code' => 1, 'message' => '获取token失败:'.$result['msg'] ];
  47. }
  48. $data = array();
  49. $data['token'] = $result['token'];
  50. $data['expire_time'] = time() + 86400 ;
  51. $data_json = json_encode( $data );
  52. S('_makeAccessToken', $data_json );
  53. $token_data = $data;
  54. }
  55. return ['code' => 0, 'token' => $token_data['token'] ];
  56. }
  57. /**
  58. * @author yj
  59. * @desc
  60. * @param int $uniacid
  61. * @return array
  62. */
  63. public function verifyToken()
  64. {
  65. $token = D('Home/Front')->get_config_by_name('localtown_mk_token');
  66. $make_url = D('Home/Front')->get_config_by_name('localtown_mk_url');
  67. $url = "{$make_url}addons/make_speed/core/public/index.php/apis/v2/verify_token";
  68. $data = array();
  69. $data['token'] = $token;
  70. $result_json = http_request($url, http_build_query($data));
  71. $result = json_decode( $result_json, true );
  72. if( empty($result) || !$result['isValid'] )
  73. {
  74. return ['code' => 1, 'message' => 'token失效' ];
  75. }
  76. return ['code' => 0, 'message' => '有效'];
  77. }
  78. /**
  79. * @author yj
  80. * @desc 获取订单详情
  81. * @param $order_num
  82. * @param int $uniacid
  83. * @return array
  84. */
  85. public function getOrderDetail( $order_num)
  86. {
  87. $token_result = $this->getMakeAccessToken();
  88. if( $token_result['code'] == 1 )
  89. {
  90. return ['code' => 1, 'message' => 'token无效' ];
  91. }
  92. $token = $token_result['token'];
  93. $make_url = D('Home/Front')->get_config_by_name('localtown_mk_url');
  94. $url = "{$make_url}addons/make_speed/core/public/index.php/apis/v2/get_order_detail";
  95. $data = [];
  96. $data['order_num'] = $order_num;
  97. $data['token'] = $token;
  98. $result_json = http_request($url, http_build_query($data));
  99. $result = json_decode( $result_json, true );
  100. if( empty($result) || $result['error_code'] > 0 )
  101. {
  102. return ['code' => 1, 'message' => $result['msg'] ];
  103. }
  104. return ['code' =>0, 'data' => $result['data'] ];
  105. }
  106. /**
  107. * @author yj
  108. * @desc 获取订单配送的状态
  109. * @param $order_num
  110. * @param int $uniacid
  111. * @return array
  112. */
  113. public function getOrderStatus( $order_num)
  114. {
  115. $token_result = $this->getMakeAccessToken();
  116. if( $token_result['code'] == 1 )
  117. {
  118. return ['code' => 1, 'message' => 'token无效' ];
  119. }
  120. $token = $token_result['token'];
  121. $make_url = D('Home/Front')->get_config_by_name('localtown_mk_url');
  122. $url = "{$make_url}addons/make_speed/core/public/index.php/apis/v2/cancel_order";
  123. $data = [];
  124. $data['order_num'] = $order_num;
  125. $data['token'] = $token;
  126. $result_json = http_request($url, http_build_query($data));
  127. $result = json_decode( $result_json, true );
  128. if( empty($result) || $result['error_code'] > 0 )
  129. {
  130. return ['code' => 1, 'message' => $result['msg'] ];
  131. }
  132. //loading = 待付款', cancel='订单已取消', payed='待接单',wait_to_shop='待到店', accepted='待取件', geted='待收件',gotoed= '待评价', completed='订单已完成');
  133. return ['code' => 0, 'status' => $result['data']['status'], 'remark' => $result['data']['remark'] ];
  134. }
  135. /**
  136. * @author yj
  137. * @desc 取消配送订单
  138. * @param $order_num
  139. * @param int $uniacid
  140. * @return array
  141. */
  142. public function cancelOrder( $order_num)
  143. {
  144. $token_result = $this->getMakeAccessToken();
  145. if( $token_result['code'] == 1 )
  146. {
  147. return ['code' => 1, 'message' => 'token无效' ];
  148. }
  149. $token = $token_result['token'];
  150. $make_url = D('Home/Front')->get_config_by_name('localtown_mk_url');
  151. $url = "{$make_url}addons/make_speed/core/public/index.php/apis/v2/cancel_order";
  152. $data = [];
  153. $data['order_num'] = $order_num;
  154. $data['token'] = $token;
  155. $result_json = http_request($url, http_build_query($data));
  156. $result = json_decode( $result_json, true );
  157. if( empty($result) || $result['error_code'] > 0 )
  158. {
  159. return ['code' => 1, 'message' => $result['msg'] ];
  160. }
  161. return ['code' => 0 ];
  162. }
  163. /**
  164. * @author yj
  165. * @desc 查询订单的配送费
  166. * @param $order_info
  167. */
  168. public function queryDeliverFee($order_info)
  169. {
  170. $fromcoord = $order_info['shipping_lat'].",".$order_info['shipping_lng'];
  171. $localtown_shop_lat = D('Home/Front')->get_config_by_name('localtown_shop_lat');
  172. $localtown_shop_lon = D('Home/Front')->get_config_by_name('localtown_shop_lon');
  173. $tocoord = $localtown_shop_lat.",".$localtown_shop_lon;
  174. $shop_id = 1;
  175. //供应商地址
  176. if($order_info['store_id'] > 0){
  177. $tocoord = $order_info['store_data']['shop_lat'].",".$order_info['store_data']['shop_lon'];
  178. //大客户版,供应商采用,否则都用平台的 todo..
  179. }
  180. $result = $this->getDeliveryPrice($fromcoord , $tocoord , $shop_id);
  181. return $result;
  182. }
  183. /**
  184. * @author yj
  185. * @desc 获取配送单价格 (运费)
  186. * @param $fromcoord
  187. * @param $tocoord
  188. * @param $shop_id
  189. */
  190. private function getDeliveryPrice($fromcoord , $tocoord , $shop_id)
  191. {
  192. $token_result = $this->getMakeAccessToken();
  193. if( $token_result['code'] == 1 )
  194. {
  195. return ['code' => 1, 'message' => 'token无效' ];
  196. }
  197. $token = $token_result['token'];
  198. $make_url = D('Home/Front')->get_config_by_name('localtown_mk_url');
  199. $url = "{$make_url}addons/make_speed/core/public/index.php/apis/v2/get_delivery_price";
  200. $data = [];
  201. $data['token'] = $token;
  202. $data['fromcoord'] = $fromcoord;
  203. $data['tocoord'] = $tocoord;
  204. $data['shop_id'] = $shop_id;
  205. $result_json = http_request($url.'?'.http_build_query($data) );
  206. $result = json_decode( $result_json, true );
  207. if( empty($result) || $result['error_code'] > 0 )
  208. {
  209. return ['code' => 1, 'message' => $result['msg'] ];
  210. }
  211. return [
  212. 'code' => 0 ,
  213. 'distance' => $result['data']['distance'],// 距离 单位km
  214. 'init' => $result['data']['init'], //起步价
  215. 'total_price' => $result['data']['total_price'], //配送价格
  216. 'mileage_price' => $result['data']['mileage_price'],//里程费
  217. 'night_price' => $result['data']['night_price'],//夜间价格
  218. 'premium' => $result['data']['premium'], //溢价
  219. ];
  220. }
  221. public function addOrder( $order_info )
  222. {
  223. $goods_name = "";
  224. foreach($order_info['goods_list'] as $k=>$v){
  225. $goods_name .= $v['goods_name'];//物品名称
  226. }
  227. $pick_time = date('Y-m-d H:i:s');
  228. $remark = "";
  229. if( !empty( $order_info['expected_delivery_time'] ) )
  230. {
  231. $remark = "预约送达时间: ".$order_info['expected_delivery_time'];
  232. }
  233. /**
  234. {
  235. "begin_detail":"万达国际B座 1918室","begin_address":"南宁市西乡塘区安吉万达广场",
  236. "begin_lat":22.873332342342342333,"begin_lng":108.2927,"begin_username":"sweetsლ",
  237. "begin_phone":"15977966741",
  238. * "end_detail":"","end_address":"南宁-东盟企业总部基地二期",
  239. "end_lat":22.866558,"end_lng":108.280449,"end_username":"星星的亮光","end_phone":"13557432464"
  240. }
  241. */
  242. //判断是否独立供应商
  243. $localtown_shop_province_id = D('Home/Front')->get_config_by_name( 'localtown_shop_province_id');
  244. $localtown_shop_city_id = D('Home/Front')->get_config_by_name( 'localtown_shop_city_id' );
  245. $localtown_shop_area_id = D('Home/Front')->get_config_by_name( 'localtown_shop_area_id');
  246. $localtown_shop_country_id = D('Home/Front')->get_config_by_name( 'localtown_shop_country_id' );
  247. $localtown_shop_address = D('Home/Front')->get_config_by_name( 'localtown_shop_address');
  248. $address_json = [];
  249. /**
  250. * $store_data = [];
  251. $store_data['address'] = $province_id.$city_id.$area_id.$country_id.$shop_address;
  252. $store_data['city'] = $city_id;
  253. $store_data['shop_lon'] = $shop_lon;
  254. $store_data['shop_lat'] = $shop_lat;
  255. $store_data['shop_telephone'] = $shop_telephone;
  256. */
  257. if( $order_info['store_id'] > 0 )
  258. {
  259. $address_json['begin_detail'] = $order_info['store_data']['begin_detail'];
  260. $address_json['begin_address'] = $order_info['store_data']['begin_address'];
  261. $address_json['begin_lat'] = $order_info['store_data']['shop_lat'];
  262. $address_json['begin_lng'] = $order_info['store_data']['shop_lon'];
  263. $address_json['begin_username'] = $order_info['store_data']['begin_username'];//todo...
  264. $address_json['begin_phone'] = $order_info['store_data']['shop_telephone'];
  265. }else{
  266. $address_json['begin_detail'] = $localtown_shop_address;
  267. $address_json['begin_address'] = $localtown_shop_province_id.$localtown_shop_city_id.$localtown_shop_area_id.$localtown_shop_country_id;
  268. $address_json['begin_lat'] = D('Home/Front')->get_config_by_name( 'localtown_shop_lat' );
  269. $address_json['begin_lng'] = D('Home/Front')->get_config_by_name( 'localtown_shop_lon');
  270. $address_json['begin_username'] = $order_info['shoname'];
  271. $address_json['begin_phone'] = D('Home/Front')->get_config_by_name( 'localtown_shop_telephone');
  272. }
  273. $address_json['end_detail'] = "";
  274. $address_json['end_address'] = $order_info['shipping_address'];
  275. $address_json['end_lat'] = $order_info['shipping_lat'];
  276. $address_json['end_lng'] = $order_info['shipping_lng'];
  277. $address_json['end_username'] = $order_info['shipping_name'];
  278. $address_json['end_phone'] = $order_info['shipping_tel'];
  279. $address = json_encode( $address_json );
  280. //lionfish_comshop_orderdistribution_order
  281. $orderdistribution_order = M('lionfish_comshop_orderdistribution_order')->where( array('order_id' => $order_info['order_id']) )->find();
  282. $pay_price = $orderdistribution_order['shipping_money'];
  283. $total_price = $orderdistribution_order['shipping_money'];
  284. $shop_id = 1;//暂时1,未来大客户版本,加上商家后台配置的
  285. $shop_domain = D('Home/Front')->get_config_by_name('shop_domain');
  286. $notify_url = $shop_domain.'make_notify.php';
  287. $result = $this-> createOrder($goods_name,$pick_time,$remark,$address,$pay_price,$total_price,$shop_id,$notify_url);
  288. return $result;
  289. }
  290. /**
  291. * @author yj
  292. * @desc 创建订单
  293. * @param $goods_name
  294. * @param $pick_time
  295. * @param $remark
  296. * @param $address
  297. * @param $pay_price
  298. * @param $total_price
  299. * @param $shop_id
  300. * @param $notify_url
  301. * @param int $uniacid
  302. * @return array
  303. */
  304. private function createOrder($goods_name,$pick_time,$remark,$address,$pay_price,$total_price,$shop_id,$notify_url )
  305. {
  306. $token_result = $this->getMakeAccessToken();
  307. if( $token_result['code'] == 1 )
  308. {
  309. return ['code' => 1, 'message' => 'token无效' ];
  310. }
  311. $token = $token_result['token'];
  312. $make_url = D('Home/Front')->get_config_by_name('localtown_mk_url');
  313. $url = "{$make_url}addons/make_speed/core/public/index.php/apis/v2/create_order";
  314. $data = [];
  315. $data['token'] = $token;
  316. $data['goods_name'] = $goods_name;
  317. $data['pick_time'] = $pick_time;
  318. $data['remark'] = $remark;
  319. $data['address'] = $address;
  320. $data['pay_price'] = $pay_price;
  321. $data['total_price'] = $total_price;
  322. $data['shop_id'] = $shop_id;
  323. $data['notify_url'] = $notify_url;
  324. $result_json = http_request($url, http_build_query($data));
  325. $result = json_decode( $result_json, true );
  326. if( empty($result) || $result['error_code'] > 0 )
  327. {
  328. return ['code' => 1, 'message' => $result['msg'] ];
  329. }
  330. $order_number = $result['data']['order_number'];
  331. return ['code' =>0, 'order_number' => $order_number ];
  332. }
  333. }
  334. ?>