ApicheckoutController.class.php 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. * 处理订单相关内容
  13. */
  14. namespace Home\Controller;
  15. class ApicheckoutController extends CommonController {
  16. protected function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->cur_page = 'apicheckout';
  20. $this->member_id = 1;
  21. $this->appid = C('weprogram_appid');
  22. $this->appsecret = C('weprogram_appscret');
  23. $this->pay_key = C('weprogram_pay_key');
  24. $this->mch_id = C('weprogram_mch_id');
  25. }
  26. /**
  27. 获取团详情
  28. **/
  29. public function group_orders()
  30. {
  31. $token = I('get.token');
  32. $order_id = I('get.id');
  33. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  34. $member_id = $weprogram_token['member_id'];
  35. $order_goods_sql = "select og.name as goods_name,g.goods_id,g.pin_count,g.pinprice,g.image,g.fan_image,g.store_id,og.pin_id from ".C('DB_PREFIX').'order_goods as og ,'.C('DB_PREFIX')."goods as g
  36. where og.order_id = {$order_id} and g.goods_id = og.goods_id limit 1";
  37. $order_goods_arr = M()->query($order_goods_sql);
  38. $order_goods = $order_goods_arr[0];
  39. if(!empty($order_goods['fan_image'])){
  40. $order_goods['image']=str_replace('http','https',C('SITE_URL')).resize($order_goods['fan_image'], C('common_image_thumb_width'), C('common_image_thumb_height'));
  41. }else {
  42. $order_goods['image']=str_replace('http','https',C('SITE_URL')).resize($order_goods['image'], C('common_image_thumb_width'), C('common_image_thumb_height'));
  43. }
  44. $water_image = '';
  45. $goods_description = M('goods_description')->where( array('goods_id' => $order_goods['goods_id']) )->find();
  46. if(!empty($goods_description['water_image']) )
  47. {
  48. $water_image = $goods_description['water_image'];
  49. }
  50. //获取拼团信息
  51. $pin_info = M('pin')->where( array('pin_id' => $order_goods['pin_id']) )->find();
  52. if($pin_info['state'] == 0 && $pin_info['end_time'] < time()){
  53. $pin_info['state'] = 2;
  54. }
  55. $tuanzhang_info = M('member')->where( array('member_id' => $pin_info['user_id']) )->find();
  56. $pin_order_sql = "select po.add_time,m.member_id,m.name,m.telephone,m.avatar from ".C('DB_PREFIX')."pin_order as po,".C('DB_PREFIX')."order as o,
  57. ".C('DB_PREFIX')."order_goods as og,".C('DB_PREFIX')."member as m
  58. where po.pin_id = ".$order_goods['pin_id']." and o.order_status_id in(1,2,4,6,7,8,9,10,11)
  59. and og.order_id = po.order_id and o.order_id = po.order_id and o.member_id= m.member_id order by po.add_time asc ";
  60. $pin_order_arr = M()->query($pin_order_sql);
  61. $users = array();
  62. $member_arr = array();
  63. foreach($pin_order_arr as $pin_order) {
  64. //{$pin_order.avatar} join_time = add_time
  65. $tmp = array();
  66. $tmp['name'] = $pin_order['name'];
  67. $tmp['avatar'] = $pin_order['avatar'];
  68. $tmp['join_time'] = $pin_order['add_time'];
  69. $users[] = $tmp;
  70. $member_arr[] = $pin_order['member_id'];
  71. }
  72. $is_me = in_array($member_id,$member_arr);
  73. $seller_info = M('seller')->field('s_id,s_true_name,s_logo')->where(array('s_id' => $order_goods['store_id']))->find();
  74. $seller_model = D('Home/Seller');
  75. $seller_info['seller_count'] = $seller_model->getStoreSellerCount($order_goods['store_id']);
  76. $order_info = '';
  77. if(!empty($water_image))
  78. {
  79. $this->share_image = str_replace('http','https',C('SITE_URL')).'/Uploads/image/'.$water_image;
  80. } else{
  81. $this->share_image = str_replace('http','https',C('SITE_URL')).$order_goods['image'];
  82. }
  83. $pinjie = M('blog')->where( array('type' => 'pinjie') )->order('blog_id desc')->find();
  84. $result = array();
  85. $result['users'] = $users;
  86. $result['status'] = $pin_info['state'];
  87. $result['order'] = $order_goods;
  88. $result['require_num'] = $order_goods['pin_count'];
  89. $result['people'] = count($pin_order_arr);
  90. $result['expire_time'] = $pin_info['end_time'];
  91. $result['take_in'] = $is_me ? 1:0;
  92. $result['pin_id'] = $pin_info['pin_id'];
  93. //expire_time {$pin_info.end_time} is_me
  94. //require_num people
  95. //$del_count = $order_goods['pin_count'] - count($pin_order_arr);
  96. echo json_encode( array('code'=>1, 'group_order' => $result) );
  97. die();
  98. }
  99. /**
  100. 微信充值
  101. **/
  102. public function wxcharge()
  103. {
  104. $token = I('get.token');
  105. $id = I('get.money');
  106. $level_info = M('member_level')->where( array('id' => $id) )->find();
  107. $money = $level_info['level_money'];
  108. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  109. $member_id = $weprogram_token['member_id'];
  110. if( empty($member_id) )
  111. {
  112. echo json_encode( array('code' =>1,'msg' =>'未登录') );
  113. die();
  114. }
  115. $member_info = M('member')->field('we_openid')->where( array('member_id' => $member_id) )->find();
  116. $member_charge_flow_data = array();
  117. $member_charge_flow_data['member_id'] = $member_id;
  118. $member_charge_flow_data['money'] = $money;
  119. $member_charge_flow_data['state'] = 0;
  120. $member_charge_flow_data['charge_time'] = 0;
  121. $member_charge_flow_data['add_time'] = time();
  122. $order_id = M('member_charge_flow')->add($member_charge_flow_data);
  123. $fee = $money;
  124. $appid = $this->appid;
  125. $body = '会员升级';
  126. $mch_id = $this->mch_id;
  127. $nonce_str = $this->nonce_str();
  128. $notify_url = C('SITE_URL').'notify.php';
  129. $openid = $member_info['we_openid'];
  130. $out_trade_no = $order_id.'-'.time().'-charge-'.$id;
  131. $spbill_create_ip = $_SERVER['REMOTE_ADDR'];
  132. $total_fee = $fee*100;
  133. $trade_type = 'JSAPI';
  134. $post['appid'] = $appid;
  135. $post['body'] = $body;
  136. $post['mch_id'] = $mch_id;
  137. $post['nonce_str'] = $nonce_str;
  138. $post['notify_url'] = $notify_url;
  139. $post['openid'] = $openid;
  140. $post['out_trade_no'] = $out_trade_no;
  141. $post['spbill_create_ip'] = $spbill_create_ip;
  142. $post['total_fee'] = $total_fee;
  143. $post['trade_type'] = $trade_type;
  144. $sign = $this->sign($post);
  145. $post_xml = '<xml>
  146. <appid>'.$appid.'</appid>
  147. <body>'.$body.'</body>
  148. <mch_id>'.$mch_id.'</mch_id>
  149. <nonce_str>'.$nonce_str.'</nonce_str>
  150. <notify_url>'.$notify_url.'</notify_url>
  151. <openid>'.$openid.'</openid>
  152. <out_trade_no>'.$out_trade_no.'</out_trade_no>
  153. <spbill_create_ip>'.$spbill_create_ip.'</spbill_create_ip>
  154. <total_fee>'.$total_fee.'</total_fee>
  155. <trade_type>'.$trade_type.'</trade_type>
  156. <sign>'.$sign.'</sign>
  157. </xml> ';
  158. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  159. $xml = $this->http_request($url,$post_xml);
  160. $array = $this->xml($xml);
  161. if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){
  162. $time = time();
  163. $tmp='';
  164. $tmp['appId'] = $appid;
  165. $tmp['nonceStr'] = $nonce_str;
  166. $tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
  167. $tmp['signType'] = 'MD5';
  168. $tmp['timeStamp'] = "$time";
  169. for($i =0; $i <3; $i++)
  170. {
  171. $tmp_data = array();
  172. $tmp_data['member_id'] = $member_id;
  173. $tmp_data['state'] = 0;
  174. $tmp_data['formid'] = $array['PREPAY_ID'];
  175. $tmp_data['addtime'] = time();
  176. M('member_formid')->add( $tmp_data );
  177. }
  178. //M('order')->where( array('order_id' => $order_id ) )->save( array('perpay_id' => $array['PREPAY_ID']) );
  179. $data['code'] = 0;
  180. $data['timeStamp'] = "$time";
  181. $data['nonceStr'] = $nonce_str;
  182. $data['signType'] = 'MD5';
  183. $data['package'] = 'prepay_id='.$array['PREPAY_ID'];
  184. $data['paySign'] = $this->sign($tmp);
  185. $data['out_trade_no'] = $out_trade_no;
  186. $data['redirect_url'] = '../dan/me';
  187. }else{
  188. $data['code'] = 1;
  189. $data['text'] = "错误";
  190. $data['RETURN_CODE'] = $array['RETURN_CODE'];
  191. $data['RETURN_MSG'] = $array['RETURN_MSG'];
  192. }
  193. echo json_encode($data);
  194. die();
  195. }
  196. /**
  197. 获取会员团列表
  198. **/
  199. public function groups()
  200. {
  201. /**
  202. "offset" : offset,
  203. "size" : size,
  204. "token" : token
  205. **/
  206. $offset = I('get.offset');
  207. $size = I('get.size');
  208. $token = I('get.token');
  209. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  210. //$member_info = M('member')->field('name,avatar')->where( array('member_id' => $weprogram_token['member_id']) )->find();
  211. $member_id = $weprogram_token['member_id'];
  212. //$type = I('get.type','0');
  213. //$this->type = $type;
  214. $pre_page = $size;
  215. $where = ' ';
  216. /**
  217. if($type == 1)
  218. {
  219. $where .= ' and p.state = 0 and p.end_time >'.time();
  220. } else if($type == 2){
  221. $where .= ' and p.state = 1 ';
  222. } else if($type == 3){
  223. $where .= ' and (p.state = 2 or (p.state =0 and p.end_time <'.time().')) ';
  224. }
  225. **/
  226. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  227. $sql = "select g.name as goods_name,g.image,g.fan_image,p.need_count,p.state,p.is_lottery,p.lottery_state,p.end_time,o.order_id,og.price,o.is_pin,o.pin_id,o.order_status_id from ".C('DB_PREFIX')."order as o, ".C('DB_PREFIX')."order_goods as og,
  228. ".C('DB_PREFIX')."pin as p,".C('DB_PREFIX')."goods as g
  229. where o.is_pin = 1 and o.order_id = og.order_id and og.goods_id = g.goods_id and o.pin_id = p.pin_id
  230. and o.member_id = ".$member_id." {$where} order by o.date_added desc limit {$offset},{$pre_page}";
  231. $list = M()->query($sql);
  232. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  233. //group_order_id goods_name image_url
  234. foreach($list as $key => $val)
  235. {
  236. $val['price'] = round($val['price'],2);
  237. if(!empty($val['fan_image'])){
  238. $val['image_url'] = str_replace('http','https',C('SITE_URL')).resize($val['fan_image'], C('common_image_thumb_width'), C('common_image_thumb_height'));
  239. }else {
  240. $val['image_url'] = str_replace('http','https',C('SITE_URL')).resize($val['image'], C('common_image_thumb_width'), C('common_image_thumb_height'));
  241. }
  242. $val['hash_order_id'] = $hashids->encode($val['order_id']);
  243. if($val['state'] == 0 && $val['end_time'] < time())
  244. {
  245. $val['state'] = 2;
  246. }
  247. //order_status_id
  248. if($val['state'] == 0){
  249. $val['state_name'] = '拼团中';
  250. }else if($val['state'] == 1){
  251. $val['state_name'] = '拼团成功';
  252. }else if($val['state'] == 2){
  253. $val['state_name'] = '拼团失败';
  254. }
  255. if($val['order_status_id'] == 3)
  256. {
  257. $val['state_name'] .= ',未付款';
  258. }
  259. $list[$key] = $val;
  260. }
  261. echo json_encode( array('code' =>1, 'group_orders' => $list) );
  262. die();
  263. }
  264. /**
  265. 会员的支付地址
  266. **/
  267. public function addresses()
  268. {
  269. $token = I('get.token');
  270. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  271. $member_id = $weprogram_token['member_id'];
  272. $member_id = 1;
  273. $address_list = M('address')->where( array('member_id' => $member_id) )->select();
  274. foreach($address_list as $key => $val)
  275. {
  276. $province_info = M('area')->field('area_name')->where( array('area_id' => $val['province_id']) )->find();
  277. $val['province'] = $province_info['area_name'];
  278. $city_info = M('area')->field('area_name')->where( array('area_id' => $val['city_id']) )->find();
  279. $val['city'] = $city_info['area_name'];
  280. $country_info = M('area')->field('area_name')->where( array('area_id' => $val['country_id']) )->find();
  281. $val['country'] = $country_info['area_name'];
  282. if($val['is_default'] == 1)
  283. {
  284. // $val['status'] = 'DEFAULT';
  285. }else {
  286. //$val['status'] = '';
  287. }
  288. $address_list[$key] = $val;
  289. }
  290. $result = array('code' => 0,'address_list' => $address_list);
  291. echo json_encode($result);
  292. die();
  293. }
  294. public function get_area_version()
  295. {
  296. $result = array('code' =>1, 'data_version' => array('version' => '1'));
  297. echo json_encode($result);
  298. die();
  299. }
  300. public function get_area()
  301. {
  302. $region_list = M('area')->field('area_id,area_name')->where(array('area_parent_id' =>0))->order('area_id asc')->select();
  303. $version = '1';
  304. $region_arr = array();
  305. foreach($region_list as $val)
  306. {
  307. $tmp_arr = array();
  308. $tmp_arr['region_id'] = $val['area_id'];
  309. $tmp_arr['region_name'] = $val['area_name'];
  310. $tmp_arr['parent_id'] = $val['area_parent_id'];
  311. $region_arr[] = $tmp_arr;
  312. }
  313. $area_list = M('area')->field('area_id,area_name,area_parent_id')->where(array('area_deep' =>2))->order('area_id asc')->select();
  314. $area_arr = array();
  315. foreach($area_list as $val)
  316. {
  317. $tmp_arr = array();
  318. $tmp_arr['region_id'] = $val['area_id'];
  319. $tmp_arr['region_name'] = $val['area_name'];
  320. $tmp_arr['parent_id'] = $val['area_parent_id'];
  321. $area_arr[] = $tmp_arr;
  322. }
  323. $stree_list = M('area')->field('area_id,area_name,area_parent_id')->where(array('area_deep' =>3))->order('area_id asc')->select();
  324. $stree_arr = array();
  325. foreach($stree_list as $val)
  326. {
  327. $tmp_arr = array();
  328. $tmp_arr['region_id'] = $val['area_id'];
  329. $tmp_arr['region_name'] = $val['area_name'];
  330. $tmp_arr['parent_id'] = $val['area_parent_id'];
  331. $stree_arr[] = $tmp_arr;
  332. }
  333. $result = array('code' =>1, 'regions' => array(0=>$region_arr,1=>$area_arr,2=>$stree_arr) );
  334. echo json_encode($result);
  335. die();
  336. }
  337. public function address_set()
  338. {
  339. $token = I('get.token');
  340. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  341. $member_id = $weprogram_token['member_id'];
  342. $address_id = I('get.address_id');
  343. if($address_id == 0)
  344. {
  345. die('xxx');
  346. }
  347. $data = array();
  348. $data['is_default'] = 0;
  349. M('address')->where( array('member_id' => $member_id) )->save( $data );
  350. $data['is_default'] = 1;
  351. M('address')->where( array('member_id' => $member_id ,'address_id' => $address_id) )->save( $data );
  352. echo json_encode( array('code' => 0) );
  353. die();
  354. }
  355. public function address_info()
  356. {
  357. $address_id = I('get.address_id');
  358. $token = I('get.token');
  359. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  360. $member_id = $weprogram_token['member_id'];
  361. $address_info = M('address')->where( array('address_id' => $address_id ,'member_id' => $member_id) )->find();
  362. //$address_info['district_id'] = $address_info['country_id'];
  363. $result = array('code' =>0, 'address' => $address_info);
  364. echo json_encode($result);
  365. die();
  366. }
  367. public function add_weixin_selftaddress()
  368. {
  369. $token = I('get.token');
  370. $data_json = file_get_contents('php://input');
  371. $data = json_decode($data_json, true);
  372. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  373. $member_id = $weprogram_token['member_id'];
  374. //{province_name: province_name, city_name: city_name, area_name: area_name, addr_tel: addr_tel, addr_detail: addr_detail}
  375. $cityName = $data['city_name'];
  376. $countyName = $data['area_name'];
  377. $detailInfo = $data['addr_detail'];
  378. $provinceName = $data['province_name'];
  379. $telNumber = $data['addr_tel'];
  380. $userName = $data['addr_name'];
  381. $sub_address_id = isset($data['sub_address_id']) ? $data['sub_address_id'] : 0 ;
  382. $province_info = M('area')->where( " area_name Like '%{$provinceName}%' " )->find();
  383. //$province_id = 35;
  384. if( !empty($province_info))
  385. {
  386. $province_id = $province_info['area_id'];
  387. }else{
  388. $area_data = array();
  389. $area_data['area_name'] = $provinceName;
  390. $area_data['area_parent_id'] = 0;
  391. $area_data['area_sort'] = 0;
  392. $area_data['area_deep'] = 1;
  393. $province_id = M('area')->add($area_data);
  394. }
  395. $city_info = M('area')->where( " area_name Like '%{$cityName}%' " )->find();
  396. //$city_id = 35;
  397. if( !empty($city_info))
  398. {
  399. $city_id = $city_info['area_id'];
  400. }else{
  401. $area_data = array();
  402. $area_data['area_name'] = $cityName;
  403. $area_data['area_parent_id'] = $province_id;
  404. $area_data['area_sort'] = 0;
  405. $area_data['area_deep'] = 2;
  406. $city_id = M('area')->add($area_data);
  407. }
  408. $country_info = M('area')->where( " area_name Like '%{$countyName}%' " )->find();
  409. //$country_id = 35;
  410. if( !empty($country_info))
  411. {
  412. $country_id = $country_info['area_id'];
  413. }else{
  414. $area_data = array();
  415. $area_data['area_name'] = $cityName;
  416. $area_data['area_parent_id'] = $city_id;
  417. $area_data['area_sort'] = 0;
  418. $area_data['area_deep'] = 3;
  419. $country_id = M('area')->add($area_data);
  420. }
  421. $address_data = array();
  422. $address_data['member_id'] = $member_id;
  423. $address_data['name'] = $userName;
  424. $address_data['telephone'] = $telNumber;
  425. $address_data['address'] = $detailInfo;
  426. $address_data['address_name'] = empty($data['address_name']) ? 'HOME' : $data['address_name'];
  427. $address_data['is_default'] = 0;
  428. $address_data['city_id'] = $city_id;
  429. $address_data['country_id'] = $country_id;
  430. $address_data['province_id'] = $province_id;
  431. if($sub_address_id > 0 )
  432. {
  433. unset($address_data['is_default']);
  434. M('address')->where( array('address_id' => $sub_address_id, 'member_id' => $member_id) )->save($address_data);
  435. $res = $sub_address_id;
  436. }else{
  437. $res = M('address')->add($address_data);
  438. }
  439. echo json_encode( array('address_id' => $res, 'code' => 0) );
  440. die();
  441. }
  442. public function add_weixinaddress()
  443. {
  444. $token = I('get.token');
  445. $data_json = file_get_contents('php://input');
  446. $data = json_decode($data_json, true);
  447. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  448. $member_id = $weprogram_token['member_id'];
  449. $cityName = $data['cityName'];
  450. $countyName = $data['countyName'];
  451. $detailInfo = $data['detailInfo'];
  452. $provinceName = $data['provinceName'];
  453. $telNumber = $data['telNumber'];
  454. $userName = $data['userName'];
  455. $province_info = M('area')->where( " area_name Like '%{$provinceName}%' " )->find();
  456. if( !empty($province_info))
  457. {
  458. $province_id = $province_info['area_id'];
  459. }else{
  460. $area_data = array();
  461. $area_data['area_name'] = $provinceName;
  462. $area_data['area_parent_id'] = 0;
  463. $area_data['area_sort'] = 0;
  464. $area_data['area_deep'] = 1;
  465. $province_id = M('area')->add($area_data);
  466. }
  467. $city_info = M('area')->where( " area_name Like '%{$cityName}%' " )->find();
  468. if( !empty($city_info))
  469. {
  470. $city_id = $city_info['area_id'];
  471. }else{
  472. $area_data = array();
  473. $area_data['area_name'] = $cityName;
  474. $area_data['area_parent_id'] = $province_id;
  475. $area_data['area_sort'] = 0;
  476. $area_data['area_deep'] = 2;
  477. $city_id = M('area')->add($area_data);
  478. }
  479. $country_info = M('area')->where( " area_name Like '%{$countyName}%' " )->find();
  480. if( !empty($country_info))
  481. {
  482. $country_id = $country_info['area_id'];
  483. }else{
  484. $area_data = array();
  485. $area_data['area_name'] = $cityName;
  486. $area_data['area_parent_id'] = $city_id;
  487. $area_data['area_sort'] = 0;
  488. $area_data['area_deep'] = 3;
  489. $country_id = M('area')->add($area_data);
  490. }
  491. $has_addre = M('address')->where( array('member_id' => $member_id,'province_id' => $province_id,'country_id' => $country_id,'city_id' => $city_id,'address' => $detailInfo,'name' => $userName, 'telephone' =>$telNumber ) )->find();
  492. if(empty($has_addre))
  493. {
  494. $has_default_address = M('address')->where( array('member_id' => $member_id, 'is_default' => 1) )->find();
  495. $address_data = array();
  496. $address_data['member_id'] = $member_id;
  497. $address_data['name'] = $userName;
  498. $address_data['telephone'] = $telNumber;
  499. $address_data['address'] = $detailInfo;
  500. $address_data['address_name'] = empty($data['address_name']) ? 'HOME' : $data['address_name'];
  501. if(!empty($has_default_address))
  502. {
  503. $address_data['is_default'] = 0;
  504. }else{
  505. $data = array();
  506. $data['is_default'] = 0;
  507. M('address')->where( array('member_id' => $member_id) )->save( $data );
  508. $address_data['is_default'] = 1;
  509. }
  510. $address_data['city_id'] = $city_id;
  511. $address_data['country_id'] = $country_id;
  512. $address_data['province_id'] = $province_id;
  513. $res = M('address')->add($address_data);
  514. }
  515. echo json_encode( array('address_id' => $res, 'code' => 0) );
  516. die();
  517. }
  518. public function modifyaddress()
  519. {
  520. $token = I('get.token');
  521. $address_id = I('get.address_id',0);
  522. $data_json = file_get_contents('php://input');
  523. $data = json_decode($data_json, true);
  524. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  525. $member_id = $weprogram_token['member_id'];
  526. $address_data = array();
  527. $address_data['member_id'] = $member_id;
  528. $address_data['name'] = $data['name'];
  529. $address_data['telephone'] = $data['telephone'];
  530. $address_data['address'] = $data['address'];
  531. $address_data['address_name'] = empty($data['address_name']) ? 'HOME' : $data['address_name'];
  532. $address_data['is_default'] = 0;
  533. $address_data['city_id'] = $data['city'];
  534. $address_data['country_id'] = $data['district'];
  535. $address_data['province_id'] = $data['province'];
  536. if( isset($address_id) && $address_id>0)
  537. {
  538. $res = M('address')->where( array('address_id' => $address_id ,'member_id' => $member_id) )->save($address_data);
  539. $res = $address_id;
  540. }else {
  541. $res = M('address')->add($address_data);
  542. }
  543. echo json_encode( array('address_id' => $res, 'code' => 0) );
  544. die();
  545. }
  546. public function address_cancle()
  547. {
  548. $token = I('get.token');
  549. $address_id = I('get.address_id',0);
  550. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  551. $member_id = $weprogram_token['member_id'];
  552. $r=M('address')->where(array('address_id'=>$address_id, 'member_id' => $member_id))->delete();
  553. echo json_encode( array('code' => 0) );
  554. die();
  555. }
  556. public function goods_detail()
  557. {
  558. $goods_id = I('get.goods_id');
  559. $goods_info = M('goods')->where( array('goods_id' => $goods_id) )->find();
  560. $goods_info = M('goods')->field('goods_id,name,pinprice,danprice,image')->where( array('goods_id'=>$goods_id) )->find();
  561. $goods_info['image_url'] = str_replace('http','https',C('SITE_URL')).'/Uploads/image/'.$goods_info['image'];
  562. $goods_info['goods_name'] = $goods_info['name'];
  563. $goods_info['group_price'] = $goods_info['pinprice'];
  564. $goods_info['alone_price'] = $goods_info['danprice'];
  565. $options=$this->get_goods_options($goods_id);
  566. $goods_option_mult_value = M('goods_option_mult_value')->where( array('goods_id' =>$goods_id) )->select();
  567. foreach($goods_option_mult_value as $key => $val)
  568. {
  569. $val['image'] = str_replace('http','https',C('SITE_URL')).resize($val['image'],200,200);
  570. $goods_option_mult_value[$key] = $val;
  571. }
  572. $result = array('code' => 1, 'goods' => $goods_info, 'options' =>$options, 'goods_option_mult_value' =>$goods_option_mult_value);
  573. echo json_encode($result);
  574. die();
  575. }
  576. public function get_goods_options($goods_id) {
  577. $result = array();
  578. $goods_option_name = array();
  579. $goods_option_data = array();
  580. $goods_option_query = M()->query("SELECT * FROM " . C('DB_PREFIX') . "goods_option po LEFT JOIN "
  581. . C('DB_PREFIX') . "option o ON po.option_id = o.option_id WHERE po.goods_id =".(int)$goods_id);
  582. foreach ($goods_option_query as $goods_option) {
  583. $goods_option_value_data = array();
  584. $goods_option_value_query = M()->query("SELECT pov.*,ov.value_name FROM " . C('DB_PREFIX')
  585. . "goods_option_value pov LEFT JOIN ". C('DB_PREFIX')
  586. ."option_value ov ON pov.option_value_id=ov.option_value_id"
  587. ." WHERE pov.goods_option_id = '"
  588. . (int)$goods_option['goods_option_id'] . "'");
  589. foreach ($goods_option_value_query as $goods_option_value) {
  590. $goods_option_value_data[] = array(
  591. 'goods_option_value_id' => $goods_option_value['goods_option_value_id'],
  592. 'option_value_id' => $goods_option_value['option_value_id'],
  593. 'quantity' => $goods_option_value['quantity'],
  594. 'name' =>$goods_option_value['value_name'],
  595. 'image' =>isset($goods_option_value['image'])?$goods_option_value['image']:'',
  596. 'price' =>'¥'.$goods_option_value['price'],
  597. 'price_prefix' => $goods_option_value['price_prefix'],
  598. );
  599. }
  600. $goods_option_name[] = $goods_option['name'];
  601. $goods_option_data[] = array(
  602. 'goods_option_id' => $goods_option['goods_option_id'],
  603. 'option_id' => $goods_option['option_id'],
  604. 'name' => $goods_option['name'],
  605. 'type' => $goods_option['type'],
  606. 'option_value' => $goods_option_value_data,
  607. 'required' => $goods_option['required']
  608. );
  609. }
  610. $result['list'] = $goods_option_data;
  611. $result['name'] = $goods_option_name;
  612. return $result;
  613. }
  614. public function sub_order()
  615. {
  616. $token = I('get.token');
  617. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  618. $member_id = $weprogram_token['member_id'];
  619. $data_json = file_get_contents('php://input');
  620. $data_s = json_decode($data_json, true);
  621. $json=array();
  622. $pay_method = $data_s['pay_method'];//支付类型
  623. $order_msg_str = $data_s['order_msg_str'];//商品订单留言
  624. $comment = $data_s['comment'];//商品订单留言
  625. $pick_up_id = $data_s['pick_up_id'];
  626. $dispatching = $data_s['dispatching'];
  627. $ziti_name = $data_s['ziti_name'];
  628. $ziti_mobile = $data_s['ziti_mobile'];
  629. $ck_yupay = $data_s['ck_yupay'];
  630. /**
  631. pick_up_id: that.data.pick_up_id,
  632. dispatching: that.data.dispatching, //express pickup
  633. ziti_name: t_ziti_name,
  634. ziti_mobile: t_ziti_mobile
  635. **/
  636. $order_msg_arr = explode('@,@', $order_msg_str);
  637. $quan_arr = $data_s['quan_arr'];//商品订单留言
  638. $order_quan_arr = array();
  639. if( !empty($quan_arr) )
  640. {
  641. foreach($quan_arr as $q_val)
  642. {
  643. $tmp_q = array();
  644. $tmp_q = explode('_',$q_val);
  645. $voucher_info = M('voucher_list')->where( array('id' =>$tmp_q[1],
  646. 'store_id' =>$tmp_q[0], 'user_id' => $member_id,'consume' =>'N','end_time' => array('gt',time() ) ) )->find();
  647. if( !empty($voucher_info) )
  648. {
  649. $order_quan_arr[$tmp_q[0]] = $tmp_q[1];
  650. }
  651. }
  652. }
  653. $msg_arr = array();
  654. foreach($order_msg_arr as $val)
  655. {
  656. $tmp_val = explode('@_@', $val);
  657. $msg_arr[ $tmp_val[0] ] = $tmp_val[1];
  658. }
  659. $cart=new \Lib\Cart();
  660. // 验证商品数量
  661. //buy_type:buy_type
  662. $buy_type = $data_s['buy_type'];//I('post.buy_type');
  663. $is_pin = 0;
  664. if($buy_type == 'pin')
  665. {
  666. $is_pin = 1;
  667. }
  668. $goodss = $cart->get_all_goodswecar($buy_type,$token);
  669. //付款人
  670. $payment=M('Member')->find($member_id);
  671. //收货人
  672. $add_where = array('member_id'=>$member_id );
  673. $address = M('address')->where( $add_where )->order('is_default desc,address_id desc')->find();
  674. $seller_goodss = array();
  675. foreach($goodss as $key => $val)
  676. {
  677. $goods_store_field = M('goods')->field('store_id')->where( array('goods_id' => $val['goods_id']) )->find();
  678. $seller_goodss[ $goods_store_field['store_id'] ][$key] = $val;
  679. }
  680. $pay_total = 0;
  681. //M('order_all')
  682. $order_all_data = array();
  683. $order_all_data['member_id'] = $member_id;
  684. $order_all_data['order_num_alias'] = build_order_no($member_id);
  685. $order_all_data['transaction_id'] = '';
  686. $order_all_data['order_status_id'] = 3;
  687. $order_all_data['is_pin'] = $is_pin;
  688. $order_all_data['paytime'] = 0;
  689. $order_all_data['addtime'] = time();
  690. $order_all_id = M('order_all')->add($order_all_data);
  691. $integral_model = D('Seller/Integral');
  692. $order_ids_arr = array();
  693. $del_integral = 0;
  694. foreach($seller_goodss as $kk => $vv)
  695. {
  696. $data = array();
  697. $data['member_id']=$member_id;
  698. $data['name']= $payment['uname'];
  699. $data['telephone']=$address['telephone'];
  700. $data['shipping_name']=$address['name'];
  701. $data['shipping_address']=$address['address'];
  702. $data['shipping_tel']=$address['telephone'];
  703. $data['shipping_province_id']=$address['province_id'];
  704. $data['shipping_city_id']=$address['city_id'];
  705. $data['shipping_country_id']=$address['country_id'];
  706. $data['shipping_method'] = 0;
  707. $data['delivery']=$dispatching;
  708. $data['pick_up_id']=$pick_up_id;
  709. $data['ziti_name']=$ziti_name;
  710. $data['ziti_mobile']=$ziti_mobile;
  711. //$pick_up_id = $data_s['pick_up_id'];
  712. //$dispatching = $data_s['dispatching'];
  713. //$ziti_name = $data_s['ziti_name'];
  714. //$ziti_mobile = $data_s['ziti_mobile'];
  715. $data['payment_method']=$pay_method;
  716. $data['address_id']= $address['address_id'];
  717. $data['voucher_id'] = isset($order_quan_arr[$kk]) ? $order_quan_arr[$kk]:0;
  718. $data['user_agent']=$_SERVER['HTTP_USER_AGENT'];
  719. $data['date_added']=time();
  720. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  721. $subject='';
  722. $fare = 0;
  723. $trans_free_toal = 0;//运费
  724. $order_total = 0;
  725. $is_lottery = 0;
  726. $is_integral = 0;
  727. foreach($goodss as $key => $good)
  728. {
  729. if($good['shipping']==1)
  730. {
  731. //统一运费
  732. $trans_free_toal += $good['goods_freight'];
  733. $trans_free = $good['goods_freight'];
  734. }else {
  735. //运费模板
  736. $trans_free = D('Home/Transport')->calc_transport($good['transport_id'], $good['quantity']*$good['weight'], $address['city_id'] );
  737. $trans_free_toal +=$trans_free;
  738. }
  739. //sku_str
  740. $order_total += $good['total'];
  741. $tp_goods_info = M('goods')->field('store_id,type')->where( array('goods_id' =>$good['goods_id'] ) )->find();
  742. //
  743. if($tp_goods_info['type'] == 'lottery')
  744. {
  745. $is_lottery = 1;
  746. }
  747. if($tp_goods_info['type'] == 'integral')
  748. {
  749. $is_integral = 1;
  750. $is_pin = 0;
  751. $check_result = $integral_model->check_user_score_can_pay($member_id, $good['sku_str'], $good['goods_id'] );
  752. if($check_result['code'] == 1)
  753. {
  754. die();
  755. }
  756. }
  757. $goods_data[] = array(
  758. 'goods_id' => $good['goods_id'],
  759. 'store_id' => $tp_goods_info['store_id'],
  760. 'name' => $good['name'],
  761. 'model' => $good['model'],
  762. 'is_pin' => $is_pin,
  763. 'pin_id' => $good['pin_id'],
  764. 'header_disc' => $good['header_disc'],
  765. 'member_disc' => $good['member_disc'],
  766. 'level_name' => $good['level_name'],
  767. 'option' => $good['sku_str'],
  768. 'quantity' => $good['quantity'],
  769. 'shipping_fare' => $trans_free,
  770. 'price' => $good['price'],
  771. 'total' => $good['total'],
  772. 'comment' => htmlspecialchars($comment)
  773. );
  774. }
  775. //$is_pin; is_lottery
  776. //'pintuan', 'normal', 'lottery'
  777. $data['type'] = 'normal';
  778. if($is_pin == 1)
  779. {
  780. $data['type'] = 'pintuan';
  781. if($is_lottery == 1)
  782. {
  783. $data['type'] = 'lottery';
  784. }
  785. }
  786. if($is_integral == 1)
  787. {
  788. $data['type'] = 'integral';
  789. $is_pin = 0;
  790. }
  791. $data['shipping_fare'] = floatval($trans_free_toal);
  792. $data['store_id']= $kk;
  793. $data['goodss'] = $goods_data;
  794. $data['order_num_alias']=build_order_no($member_id);
  795. $data['totals'][0]=array(
  796. 'code'=>'sub_total',
  797. 'title'=>'商品价格',
  798. 'text'=>'¥'.$order_total,
  799. 'value'=>$order_total
  800. );
  801. $data['totals'][1]=array(
  802. 'code'=>'shipping',
  803. 'title'=>'运费',
  804. 'text'=>'¥'.$trans_free_toal,
  805. 'value'=>$trans_free_toal
  806. );
  807. $data['totals'][2]=array(
  808. 'code'=>'total',
  809. 'title'=>'总价',
  810. 'text'=>'¥'.($order_total+$trans_free_toal),
  811. 'value'=>($order_total+$trans_free_toal)
  812. );
  813. $data['from_type'] = 'wepro';
  814. if($data['voucher_id'] > 0) {
  815. $voucher_info = M('voucher_list')->where( array('id' => $data['voucher_id']) )->find();
  816. $data['voucher_credit'] = $voucher_info['credit'];
  817. M('voucher_list')->where( array('id' => $data['voucher_id']) )->save( array('consume' => 'Y') );
  818. } else {
  819. $data['voucher_credit'] = 0;
  820. }
  821. $data['comment'] = htmlspecialchars($comment);
  822. //判断自提 dispatching:"pickup"
  823. //dispatching, //express pickup
  824. if($dispatching == 'express')
  825. {
  826. $data['total']=($order_total+$fare - $data['voucher_credit']);
  827. }else{
  828. $data['total'] = ($order_total - $data['voucher_credit']);
  829. }
  830. //积分商城
  831. if($data['type'] == 'integral')
  832. {
  833. $del_integral += $order_total;//扣除积分
  834. $data['total'] = 0;
  835. $order_total = 0;
  836. }
  837. //$data['total']=($order_total+$trans_free_toal- $data['voucher_credit'] );
  838. $oid=D('Order')->addOrder($data);
  839. //delivery pickup pick_up_id
  840. if($data['delivery'] == 'pickup')
  841. {
  842. $verify_bool = true;
  843. $verifycode = 0;
  844. while($verify_bool)
  845. {
  846. $code = (ceil(time()/100)+rand(10000000,40000000)).rand(1000,9999);
  847. $verifycode = $code ? $code : rand(100000,999999);
  848. $verifycode = str_replace('1989','9819',$verifycode);
  849. $verifycode = str_replace('1259','9521',$verifycode);
  850. $verifycode = str_replace('12590','95210',$verifycode);
  851. $verifycode = str_replace('10086','68001',$verifycode);
  852. $pick_order = M('pick_order')->where( array('pick_sn' => $verifycode) )->find();
  853. if(empty($pick_order))
  854. {
  855. $verify_bool = false;
  856. }
  857. }
  858. $pick_data = array();
  859. $pick_data['pick_sn'] = $verifycode;
  860. $pick_data['pick_id'] = $pick_up_id;
  861. $pick_data['order_id'] = $oid;
  862. $pick_data['state'] = 0;
  863. $pick_data['ziti_name'] = $ziti_name;
  864. $pick_data['ziti_mobile'] = $ziti_mobile;
  865. $pick_data['addtime'] = time();
  866. M('pick_order')->add($pick_data);
  867. }
  868. $order_ids_arr[] = $oid;
  869. //$pay_total = $pay_total + $order_total+$trans_free_toal - $data['voucher_credit'];
  870. if($dispatching == 'express')
  871. {
  872. $pay_total = $pay_total + $order_total+$trans_free_toal - $data['voucher_credit'];
  873. }else{
  874. $pay_total = $pay_total + $order_total - $data['voucher_credit'];
  875. }
  876. $order_relate_data = array();
  877. $order_relate_data['order_all_id'] = $order_all_id;
  878. $order_relate_data['order_id'] = $oid;
  879. $order_relate_data['addtime'] = time();
  880. M('order_relate')->add($order_relate_data);
  881. }
  882. M('order_all')->where( array('id' => $order_all_id) )->save( array('total_money' => $pay_total) );
  883. if($order_all_id){
  884. //direct suborder
  885. $order = M('order')->where(array( 'order_id' => $oid ))->find();
  886. $member_info = M('member')->field('we_openid,account_money')->where( array('member_id' => $member_id) )->find();
  887. if( $pay_total<=0 || ($ck_yupay == 1 && $member_info['account_money'] >= $pay_total) )
  888. {
  889. //检测是否需要扣除积分
  890. //var_dump($del_integral,$is_integral );die();
  891. if($del_integral> 0 && $is_integral == 1)
  892. {
  893. //
  894. $integral_model->charge_member_score( $member_id, $del_integral,'out', 'orderbuy', $oid);
  895. }
  896. if($ck_yupay == 1 && $pay_total >0)
  897. {
  898. //开始余额支付
  899. $member_charge_flow_data = array();
  900. $member_charge_flow_data['member_id'] = $member_id;
  901. $member_charge_flow_data['trans_id'] = $oid;
  902. $member_charge_flow_data['money'] = $pay_total;
  903. $member_charge_flow_data['state'] = 3;
  904. $member_charge_flow_data['charge_time'] = time();
  905. $member_charge_flow_data['add_time'] = time();
  906. M('member_charge_flow')->add($member_charge_flow_data);
  907. //开始处理扣钱
  908. M('member')->where( array('member_id' => $member_id) )->setInc('account_money',-$pay_total);
  909. }
  910. //开始处理订单状态
  911. $order_all = M('order_all')->where( array('id' => $order_all_id) )->find();
  912. if($order&&($order['order_status_id']!=C('paid_order_status_id')))
  913. {
  914. //支付完成
  915. $o = array();
  916. $o['order_status_id'] = $order['is_pin'] == 1 ? 2:1;
  917. $o['paytime']=time();
  918. $o['transaction_id'] = $transaction_id;
  919. M('order_all')->where( array('id' => $out_trade_no) )->save($o);
  920. $order_relate_list = M('order_relate')->where( array('order_all_id' => $order_all['id']) )->select();
  921. foreach($order_relate_list as $order_relate)
  922. {
  923. $order=M('Order')->where( array('order_id' =>$order_relate['order_id']) )->find();
  924. if( $order && $order['order_status_id'] == 3)
  925. {
  926. $o = array();
  927. $o['payment_code'] = 'yuer';
  928. $o['order_id']=$order['order_id'];
  929. $o['order_status_id'] = $order['is_pin'] == 1 ? 2:1;
  930. $o['date_modified']=time();
  931. $o['pay_time']=time();
  932. $o['transaction_id'] = $is_integral ==1? '积分兑换':'余额支付';
  933. M('Order')->save($o);
  934. $kucun_method = C('kucun_method');
  935. $kucun_method = empty($kucun_method) ? 0 : intval($kucun_method);
  936. $goods_model = D('Home/Goods');
  937. if($kucun_method == 1)
  938. {//支付完减库存,增加销量
  939. $order_goods_list = M('order_goods')->where( array('order_id' => $order['order_id']) )->select();
  940. foreach($order_goods_list as $order_goods)
  941. {
  942. $goods_model->del_goods_mult_option_quantity($order['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity'],1);
  943. }
  944. }
  945. $oh = array();
  946. $oh['order_id']=$order['order_id'];
  947. $oh['order_status_id']= $order['is_pin'] == 1 ? 2:1;
  948. $oh['comment']='买家已付款';
  949. $oh['date_added']=time();
  950. $oh['notify']=1;
  951. M('OrderHistory')->add($oh);
  952. //发送购买通知
  953. $weixin_nofity = D('Home/Weixinnotify');
  954. $weixin_nofity->orderBuy($order['order_id']);
  955. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  956. $order_id = $hashids->encode($order['order_id']);
  957. //group_order_id
  958. if($order['is_pin'] == 1)
  959. {
  960. $pin_order = M('pin_order')->where(array('order_id' => $order['order_id']) )->find();
  961. $pin_model = D('Home/Pin');
  962. $pin_model->insertNotifyOrder($order['order_id']);
  963. $is_pin_success = $pin_model->checkPinSuccess($pin_order['pin_id']);
  964. if($is_pin_success) {
  965. //todo send pintuan success notify
  966. $pin_model->updatePintuanSuccess($pin_order['pin_id']);
  967. }
  968. }
  969. }
  970. }
  971. //返回支付成功给app
  972. $data = array();
  973. $data['code'] = 0;
  974. $data['has_yupay'] = 1;
  975. $data['is_integral'] = $is_integral;
  976. }
  977. }else{
  978. $fee = $pay_total;
  979. $appid = $this->appid;
  980. $body = '商品购买';
  981. $mch_id = $this->mch_id;
  982. $nonce_str = $this->nonce_str();
  983. $notify_url = C('SITE_URL').'notify.php';
  984. $openid = $member_info['we_openid'];
  985. $out_trade_no = $order_all_id.'-'.time();
  986. $spbill_create_ip = $_SERVER['REMOTE_ADDR'];
  987. $total_fee = $fee*100;
  988. $trade_type = 'JSAPI';
  989. $post['appid'] = $appid;
  990. $post['body'] = $body;
  991. $post['mch_id'] = $mch_id;
  992. $post['nonce_str'] = $nonce_str;
  993. $post['notify_url'] = $notify_url;
  994. $post['openid'] = $openid;
  995. $post['out_trade_no'] = $out_trade_no;
  996. $post['spbill_create_ip'] = $spbill_create_ip;
  997. $post['total_fee'] = $total_fee;
  998. $post['trade_type'] = $trade_type;
  999. $sign = $this->sign($post);
  1000. $post_xml = '<xml>
  1001. <appid>'.$appid.'</appid>
  1002. <body>'.$body.'</body>
  1003. <mch_id>'.$mch_id.'</mch_id>
  1004. <nonce_str>'.$nonce_str.'</nonce_str>
  1005. <notify_url>'.$notify_url.'</notify_url>
  1006. <openid>'.$openid.'</openid>
  1007. <out_trade_no>'.$out_trade_no.'</out_trade_no>
  1008. <spbill_create_ip>'.$spbill_create_ip.'</spbill_create_ip>
  1009. <total_fee>'.$total_fee.'</total_fee>
  1010. <trade_type>'.$trade_type.'</trade_type>
  1011. <sign>'.$sign.'</sign>
  1012. </xml> ';
  1013. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  1014. $xml = $this->http_request($url,$post_xml);
  1015. $array = $this->xml($xml);
  1016. //var_dump($openid, $array);die();
  1017. if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){
  1018. $time = time();
  1019. $tmp='';
  1020. $tmp['appId'] = $appid;
  1021. $tmp['nonceStr'] = $nonce_str;
  1022. $tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
  1023. $tmp['signType'] = 'MD5';
  1024. $tmp['timeStamp'] = "$time";
  1025. M('order')->where( array('order_id' => array('in',$order_ids_arr) ) )->save( array('perpay_id' => (string)$array['PREPAY_ID']) );
  1026. $data = array();
  1027. $data['code'] = 0;
  1028. $data['appid'] = $appid;
  1029. $data['timeStamp'] = "$time";
  1030. $data['nonceStr'] = $nonce_str;
  1031. $data['signType'] = 'MD5';
  1032. $data['package'] = 'prepay_id='.$array['PREPAY_ID'];
  1033. $data['paySign'] = $this->sign($tmp);
  1034. $data['out_trade_no'] = $out_trade_no;
  1035. if($is_pin == 1)
  1036. {
  1037. $data['redirect_url'] = '../groups/group?id='.$oid.'&is_show=1';
  1038. } else {
  1039. $data['redirect_url'] = '../orders/order_show_all?order_all_id=' + $order_all_id;
  1040. }
  1041. }else{
  1042. $data = array();
  1043. $data['code'] = 1;
  1044. $data['text'] = "错误";
  1045. $data['RETURN_CODE'] = $array['RETURN_CODE'];
  1046. $data['RETURN_MSG'] = $array['RETURN_MSG'];
  1047. }
  1048. $data['has_yupay'] = 0;
  1049. }
  1050. if($is_pin == 1)
  1051. {
  1052. $data['order_id'] = $oid;
  1053. $data['order_all_id'] = $order_all_id;
  1054. }else{
  1055. $data['order_id'] = $oid;
  1056. $data['order_all_id'] = $order_all_id;
  1057. }
  1058. echo json_encode($data);
  1059. die();
  1060. }else{
  1061. echo json_encode( array('code' =>1,'order_all_id' =>$order_all_id) );
  1062. die();
  1063. }
  1064. }
  1065. public function orders2()
  1066. {
  1067. $token = I('get.token');
  1068. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  1069. $member_id = $weprogram_token['member_id'];
  1070. $data_json = file_get_contents('php://input');
  1071. $data = json_decode($data_json, true);
  1072. $quantity = $data['quantity'];
  1073. $goods_sku = $data['goods_sku'];
  1074. if( !empty($goods_sku) )
  1075. {
  1076. $option = array_filter($goods_sku);
  1077. $new_option = array();
  1078. foreach($option as $heng_hua)
  1079. {
  1080. $tmp_a = explode('_',$heng_hua);
  1081. $new_option[$tmp_a[0]] = $tmp_a[1];
  1082. }
  1083. $option = $new_option;
  1084. }
  1085. $goods_id = $data['goods_id'];
  1086. $address_id = $data['address_id'];
  1087. $group_order_id = $data['group_order_id'];
  1088. $groupbuy = $data['groupbuy'] == 1 ? 'pin':'dan';
  1089. $member_info = M('member')->field('name')->where( array('member_id' => $member_id) )->find();
  1090. $shipping = M('address')->where( array('address_id'=>$address_id) )->find();
  1091. $data['member_id']=$member_id;
  1092. $data['name']=$member_info['name'];
  1093. $data['telephone']=$shipping['telephone'];
  1094. $data['shipping_name']=$shipping['name'];
  1095. $data['shipping_address']=$shipping['address'];
  1096. $data['shipping_tel']=$shipping['telephone'];
  1097. $data['shipping_province_id']=$shipping['province_id'];
  1098. $data['shipping_city_id']=$shipping['city_id'];
  1099. $data['shipping_country_id']=$shipping['country_id'];
  1100. $data['shipping_method'] = 0;
  1101. $data['delivery']='express';
  1102. $data['payment_method']='wxpay';
  1103. $data['address_id']=$address_id;
  1104. $data['voucher_id']=0;
  1105. $data['user_agent']=$_SERVER['HTTP_USER_AGENT'];
  1106. $data['date_added']=time();
  1107. $data['comment']='';
  1108. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  1109. $goodss = M('goods')->where( array('goods_id' =>$goods_id) )->select();
  1110. $t=0;
  1111. foreach ($goodss as $goods)
  1112. {
  1113. $option_data = array();
  1114. //sku begin
  1115. if( !empty($option) )
  1116. {
  1117. //$option
  1118. $option_data = array();
  1119. $options = $option;
  1120. foreach ($options as $goods_option_id => $option_value) {
  1121. $option_query = M()->query("SELECT po.goods_option_id, po.option_id, o.name, o.type FROM "
  1122. . C('DB_PREFIX') . "goods_option po LEFT JOIN `"
  1123. . C('DB_PREFIX') . "option` o ON (po.option_id = o.option_id)
  1124. WHERE po.goods_option_id = '"
  1125. . (int)$goods_option_id . "' AND po.goods_id = " . (int)$goods_id);
  1126. if ($option_query) {
  1127. $option_value_query = M()->query("SELECT pov.option_value_id,
  1128. ov.value_name, pov.quantity, pov.subtract, pov.price, pov.price_prefix,pov.weight, pov.weight_prefix FROM "
  1129. . C('DB_PREFIX') . "goods_option_value pov LEFT JOIN "
  1130. . C('DB_PREFIX') . "option_value ov ON (pov.option_value_id = ov.option_value_id) WHERE pov.goods_option_value_id = '"
  1131. . (int)$option_value . "' AND pov.goods_option_id = "
  1132. . (int)$goods_option_id);
  1133. if ($option_value_query) {
  1134. $option_data[] = array(
  1135. 'goods_option_id' => $goods_option_id,
  1136. 'goods_option_value_id' => $option_value,
  1137. 'option_id' => $option_query[0]['option_id'],
  1138. 'option_value_id' => $option_value_query[0]['option_value_id'],
  1139. 'name' => $option_query[0]['name'],
  1140. 'value' => $option_value_query[0]['value_name'],
  1141. 'type' => $option_query[0]['type'],
  1142. 'quantity' => $option_value_query[0]['quantity'],
  1143. 'subtract' => $option_value_query[0]['subtract'],
  1144. 'price' => $option_value_query[0]['price'],
  1145. 'price_prefix' => 0,
  1146. 'weight' => 0,
  1147. 'weight_prefix' => 0
  1148. );
  1149. }
  1150. }
  1151. }
  1152. }
  1153. /**
  1154. foreach ($goods['option'] as $option) {
  1155. $value = $option['value'];
  1156. $option_data[] = array(
  1157. 'goods_option_id' => $option['goods_option_id'],
  1158. 'goods_option_value_id' => $option['goods_option_value_id'],
  1159. 'option_id' => $option['option_id'],
  1160. 'option_value_id' => $option['option_value_id'],
  1161. 'name' => $option['name'],
  1162. 'value' => $value,
  1163. 'type' => $option['type']
  1164. );
  1165. }
  1166. **/
  1167. $tp_goods_info = M('goods')->field('store_id,express_list')->where( array('goods_id' =>$goods['goods_id']))->find();
  1168. $express_list_arr = unserialize($tp_goods_info['express_list']);
  1169. if($data['delivery'] == 'express')
  1170. {
  1171. $fare = isset($express_list_arr[$data['shipping_method']]) ? $express_list_arr[$data['shipping_method']]['price'] : 0;
  1172. }
  1173. $t+=$goods['pinprice'];
  1174. $goods['pinprice'] = $goods['pinprice'];
  1175. //$goods_id=$hashids->encode($goods['goods_id']);
  1176. $goods_data[] = array(
  1177. 'goods_id' => $hashids->encode($goods['goods_id']),//goods_id
  1178. 'name' => $goods['name'],
  1179. 'store_id' => $tp_goods_info['store_id'],
  1180. 'model' => $goods['model'],
  1181. 'option' => $option_data,
  1182. 'quantity' => 1,
  1183. 'pin_type' => $groupbuy,
  1184. 'pin_id' => $group_order_id,
  1185. 'price' => $goods['pinprice'],
  1186. 'total' => $goods['pinprice']
  1187. );
  1188. $subject.=$goods['name'].' ';
  1189. }
  1190. $data['shipping_fare'] = floatval($fare);
  1191. $data['total']=($t+$fare );
  1192. $data['goodss'] = $goods_data;
  1193. $data['order_num_alias']=build_order_no($data['member_id']);
  1194. $data['totals'][0]=array(
  1195. 'code'=>'sub_total',
  1196. 'title'=>'商品价格',
  1197. 'text'=>'¥'.$t,
  1198. 'value'=>$t
  1199. );
  1200. $data['totals'][1]=array(
  1201. 'code'=>'shipping',
  1202. 'title'=>'运费',
  1203. 'text'=>'¥'.$fare,
  1204. 'value'=>$fare
  1205. );
  1206. $data['totals'][2]=array(
  1207. 'code'=>'voucher',
  1208. 'title'=>'优惠券',
  1209. 'text'=>'¥'.$data['voucher_credit'],
  1210. 'value'=>$data['voucher_credit']
  1211. );
  1212. $data['totals'][3]=array(
  1213. 'code'=>'total',
  1214. 'title'=>'总价',
  1215. 'text'=>'¥'.($t+$fare- $data['voucher_credit']),
  1216. 'value'=>($t+$fare- $data['voucher_credit'])
  1217. );
  1218. $oid=D('Order')->addOrder($data);
  1219. //self.order_id = data.order_id;
  1220. $result = array('code' =>1,'order_id' =>$oid);
  1221. echo json_encode($result);
  1222. die();
  1223. }
  1224. public function wxpay()
  1225. {
  1226. $token = I('get.token');
  1227. $order_id = I('get.order_id');
  1228. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  1229. $member_id = $weprogram_token['member_id'];
  1230. if( empty($member_id) )
  1231. {
  1232. echo json_encode( array('code' =>1,'msg' =>'未登录') );
  1233. die();
  1234. }
  1235. $member_info = M('member')->field('we_openid')->where( array('member_id' => $member_id) )->find();
  1236. $order = M('order')->where(array( 'order_id' => $order_id ))->find();
  1237. //var_dump($order);die(); is_pin
  1238. //$order['is_pin']
  1239. $pin_order = M('pin_order')->where( array('order_id' => $order_id) )->find();
  1240. if( !empty($pin_order) )
  1241. {
  1242. $pin_model = D('Home/Pin');
  1243. $is_pin_over = $pin_model->getNowPinState($pin_order['pin_id']);
  1244. if($is_pin_over != 0)
  1245. {
  1246. M('pin_order')->where( array('order_id' => $order_id) )->delete();
  1247. M('pin')->where( array('pin_id' => $pin_order['pin_id'], 'order_id' => $order_id) )->delete();
  1248. $order_goods_info = M('order_goods')->field('goods_id')->where( array('order_id'=>$order_id) )->find();
  1249. //新开团
  1250. $pin_id = $pin_model->openNewTuan($order_id,$order_goods_info['goods_id'],$member_id);
  1251. //插入拼团订单
  1252. $pin_model->insertTuanOrder($pin_id,$order_id);
  1253. }
  1254. }
  1255. //单独支付一个店铺的订单
  1256. M('order_relate')->where( array('order_id' => $order_id) )->delete();
  1257. $order_all_data = array();
  1258. $order_all_data['member_id'] = $member_id;
  1259. $order_all_data['order_num_alias'] = build_order_no($member_id);
  1260. $order_all_data['transaction_id'] = '';
  1261. $order_all_data['order_status_id'] = 3;
  1262. $order_all_data['is_pin'] = $order['is_pin'];
  1263. $order_all_data['paytime'] = 0;
  1264. $order_all_data['total_money'] = $order['total'];
  1265. $order_all_data['addtime'] = time();
  1266. $order_all_id = M('order_all')->add($order_all_data);
  1267. $order_relate_data = array();
  1268. $order_relate_data['order_all_id'] = $order_all_id;
  1269. $order_relate_data['order_id'] = $order_id;
  1270. $order_relate_data['addtime'] = time();
  1271. M('order_relate')->add($order_relate_data);
  1272. //$order_all_data[order_num_alias] shipping_fare
  1273. //order $data['delivery'] == 'pickup'
  1274. if( $order['delivery'] == 'pickup' )
  1275. {
  1276. $fee = $order['total'];//-$order['voucher_credit'];
  1277. }else {
  1278. $fee = $order['total'];//+$order['shipping_fare']-$order['voucher_credit'];
  1279. }
  1280. /**
  1281. $pay_total = $pay_total + $order_total+$trans_free_toal - $data['voucher_credit'];
  1282. **/
  1283. $appid = $this->appid;
  1284. $body = '商品购买';
  1285. $mch_id = $this->mch_id;
  1286. $nonce_str = $this->nonce_str();
  1287. $notify_url = C('SITE_URL').'notify.php';
  1288. $openid = $member_info['we_openid'];
  1289. $out_trade_no = $order_all_id.'-'.time();
  1290. $spbill_create_ip = $_SERVER['REMOTE_ADDR'];
  1291. $total_fee = $fee*100;
  1292. $trade_type = 'JSAPI';
  1293. $post['appid'] = $appid;
  1294. $post['body'] = $body;
  1295. $post['mch_id'] = $mch_id;
  1296. $post['nonce_str'] = $nonce_str;
  1297. $post['notify_url'] = $notify_url;
  1298. $post['openid'] = $openid;
  1299. $post['out_trade_no'] = $out_trade_no;
  1300. $post['spbill_create_ip'] = $spbill_create_ip;
  1301. $post['total_fee'] = $total_fee;
  1302. $post['trade_type'] = $trade_type;
  1303. $sign = $this->sign($post);
  1304. $post_xml = '<xml>
  1305. <appid>'.$appid.'</appid>
  1306. <body>'.$body.'</body>
  1307. <mch_id>'.$mch_id.'</mch_id>
  1308. <nonce_str>'.$nonce_str.'</nonce_str>
  1309. <notify_url>'.$notify_url.'</notify_url>
  1310. <openid>'.$openid.'</openid>
  1311. <out_trade_no>'.$out_trade_no.'</out_trade_no>
  1312. <spbill_create_ip>'.$spbill_create_ip.'</spbill_create_ip>
  1313. <total_fee>'.$total_fee.'</total_fee>
  1314. <trade_type>'.$trade_type.'</trade_type>
  1315. <sign>'.$sign.'</sign>
  1316. </xml> ';
  1317. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  1318. $xml = $this->http_request($url,$post_xml);
  1319. $array = $this->xml($xml);
  1320. if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){
  1321. $time = time();
  1322. $tmp='';
  1323. $tmp['appId'] = $appid;
  1324. $tmp['nonceStr'] = $nonce_str;
  1325. $tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
  1326. $tmp['signType'] = 'MD5';
  1327. $tmp['timeStamp'] = "$time";
  1328. M('order')->where( array('order_id' => $order_id ) )->save( array('perpay_id' => $array['PREPAY_ID']) );
  1329. $data['code'] = 0;
  1330. $data['timeStamp'] = "$time";
  1331. $data['nonceStr'] = $nonce_str;
  1332. $data['signType'] = 'MD5';
  1333. $data['package'] = 'prepay_id='.$array['PREPAY_ID'];
  1334. $data['paySign'] = $this->sign($tmp);
  1335. $data['out_trade_no'] = $out_trade_no;
  1336. $data['is_pin'] = $order['is_pin'];
  1337. if($order['is_pin'] == 1)
  1338. {
  1339. $data['redirect_url'] = '../groups/group?id='.$order_id.'&is_show=1';
  1340. } else {
  1341. $data['redirect_url'] = '../orders/order?id=' + $order_id;
  1342. }
  1343. }else{
  1344. $data['code'] = 1;
  1345. $data['text'] = "错误";
  1346. $data['RETURN_CODE'] = $array['RETURN_CODE'];
  1347. $data['RETURN_MSG'] = $array['RETURN_MSG'];
  1348. }
  1349. echo json_encode($data);
  1350. die();
  1351. }
  1352. public function getorder()
  1353. {
  1354. $token = I('get.token');
  1355. $order_id = I('get.order_id',0);
  1356. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  1357. $member_id = $weprogram_token['member_id'];
  1358. $order_info = M('order')->where( array('order_id' => $order_id) )->find();
  1359. $order_status_info = M('order_status')->where( array('order_status_id' => $order_info['order_status_id']) )->find();
  1360. $shipping_province = M('area')->where( array('area_id' => $order_info['shipping_province_id']) )->find();
  1361. $shipping_city = M('area')->where( array('area_id' => $order_info['shipping_city_id']) )->find();
  1362. $shipping_country = M('area')->where( array('area_id' => $order_info['shipping_country_id']) )->find();
  1363. $order_goods = M('order_goods')->where( array('order_id' => $order_id) )->find();
  1364. $store_info = M('seller')->where('s_id='.$order_goods['store_id'])->find();
  1365. $order_option_info = M('order_option')->where( array('order_id' =>$order_id) )->select();
  1366. $goods_info = M('goods')->where( array('goods_id' => $order_goods['goods_id']) )->find();
  1367. $voucher_info = array();
  1368. if($order_info['voucher_id'] > 0) {
  1369. $voucher_info = M('voucher_list')->where( array('id' => $order_info['voucher_id']) )->find();
  1370. }
  1371. $pin_model = D('Home/Pin');
  1372. if($order_info['order_status_id'] == 2)
  1373. {
  1374. if($order_info['is_pin'] == 1 && $order_info['pin_id'] > 0)
  1375. {
  1376. $state = $pin_model->getNowPinState($order_info['pin_id']);
  1377. if($state == 2){
  1378. $order_status_info['name'] = '拼团失败,等待退款';
  1379. }
  1380. }
  1381. }
  1382. if($order_info['order_status_id'] == 1)
  1383. {
  1384. if($order_info['is_pin'] == 1 && $order_info['pin_id'] > 0 && $order_info['lottery_win'] ==0)
  1385. {
  1386. $pin_info = M('pin')->where( array('pin_id' =>$order_info['pin_id'] ) )->find();
  1387. if($pin_info['is_lottery'] == 1)
  1388. {
  1389. if($pin_info['lottery_state'] == 0){
  1390. $order_status_info['name'] = '已成团,待抽奖';
  1391. }else if($pin_info['lottery_state'] == 1){
  1392. $order_status_info['name'] = '二等奖,待退款并送券';
  1393. }
  1394. }
  1395. }else if($order_info['is_pin'] == 1 && $order_info['pin_id'] > 0 && $order_info['lottery_win'] ==1)
  1396. {
  1397. $order_status_info['name'] = '一等奖,待发货';
  1398. }
  1399. }
  1400. $pick_order_info = array();
  1401. $pick_up = array();
  1402. if($order_info['delivery'] == 'pickup')
  1403. {
  1404. $pick_order_info = M('pick_order')->where( array('order_id' => $order_info['order_id']) )->find();
  1405. $pick_up = M('pick_up')->where( array('id' => $pick_order_info['pick_id']) )->find();
  1406. }
  1407. $this->pick_order_info = $pick_order_info;
  1408. $this->pick_up = $pick_up;
  1409. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  1410. $order_info['hash_order_id']= $hashids->encode($order_info['order_id']);
  1411. $order_send_history = M('order_history')->where( array('order_id' => $order_id,'order_status_id' =>4) )->find();
  1412. $order_get_history = M('order_history')->where( array('order_id' => $order_id,'order_status_id' =>6) )->find();
  1413. $order_infos = array();
  1414. $order_infos = array_merge($order_infos,$order_info);
  1415. $order_infos['order_time'] = $order_info['date_modified'];
  1416. $order_infos['order_status'] = $order_info['order_status_id'];
  1417. $order_infos['order_amount'] = round($order_info['total'],2);
  1418. $order_infos['city_name'] = $shipping_province['area_name'].$shipping_city['area_name'].$shipping_country['area_name'];
  1419. $order_infos['receive_name'] = $order_infos['shipping_name'];
  1420. $order_infos['mobile'] = $order_infos['shipping_tel'];
  1421. $order_infos['order_sn'] = $order_infos['order_num_alias'];
  1422. $order_infos['order_option_info'] = $order_option_info;
  1423. $order_infos['image_url'] = str_replace('http','https',C('SITE_URL')).'/'.resize($goods_info['image'], C('common_image_thumb_width'), C('common_image_thumb_height'));
  1424. $order_infos['goods_name'] = $goods_info['name'];
  1425. $order_infos['market_price'] = $order_goods['total'];
  1426. $order_infos['group_order_id'] = $order_info['pin_id'];
  1427. $order_infos['goods_id'] = $order_goods['goods_id'];
  1428. $result = array('code'=>1,'order' => $order_infos);
  1429. echo json_encode($result);
  1430. die();
  1431. }
  1432. public function orderlist()
  1433. {
  1434. $token = I('get.token');
  1435. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  1436. $member_id = $weprogram_token['member_id'];
  1437. $page = I('get.page',1);
  1438. $size = I('get.size',6);
  1439. $offset = ($page - 1)* $size;
  1440. $type = I('get.type','');
  1441. $order_status = I('get.order_status',-1);
  1442. $where = ' and o.member_id = '.$member_id;
  1443. /**
  1444. if($order_status == 5)
  1445. {
  1446. $where .= ' and o.is_pin = 1 and o.order_status_id = 7';
  1447. }
  1448. else if($order_status == 4)
  1449. {
  1450. $where .= ' and o.is_pin = 1 and o.order_status_id in( 1,4,6,11) ';
  1451. }
  1452. else
  1453. **/
  1454. if($order_status > 0 && $order_status <12)
  1455. {
  1456. $where .= ' and o.order_status_id = '.$order_status;
  1457. }
  1458. else if($order_status == 12)
  1459. {
  1460. $where .= ' and o.order_status_id in(12,13)';
  1461. }
  1462. if( !empty($type) )
  1463. {
  1464. $where .= ' and o.type = "integral" ';
  1465. }
  1466. $sql = "select o.order_id,o.delivery,o.is_pin,o.is_zhuli,o.shipping_fare,o.voucher_credit,o.store_id,o.total,o.order_status_id,o.lottery_win,o.type,os.name as status_name from ".C('DB_PREFIX')."order as o ,
  1467. ".C('DB_PREFIX')."order_status as os
  1468. where o.order_status_id = os.order_status_id {$where}
  1469. order by o.date_added desc limit {$offset},{$size}";
  1470. $list = M()->query($sql);
  1471. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  1472. foreach($list as $key => $val)
  1473. {
  1474. //voucher_credit shipping_fare
  1475. if($val['delivery'] == 'pickup')
  1476. {
  1477. //$val['total'] = round($val['total'],2) - round($val['voucher_credit'],2);
  1478. }else{
  1479. //$val['total'] = round($val['total'],2)+round($val['shipping_fare'],2) - round($val['voucher_credit'],2);
  1480. }
  1481. // $val['delivery'] =='pickup'
  1482. if($val['shipping_fare']<=0.001 || $val['delivery'] == 'pickup')
  1483. {
  1484. $val['shipping_fare'] = '免运费';
  1485. }else{
  1486. $val['shipping_fare'] = '运费:'.$val['shipping_fare'];
  1487. }
  1488. if($val['order_status_id'] == 10)
  1489. {
  1490. $val['status_name'] = '等待退款';
  1491. }
  1492. else if($val['order_status_id'] == 4 && $val['delivery'] =='pickup')
  1493. {
  1494. //delivery 6
  1495. $val['status_name'] = '待自提';
  1496. //已自提
  1497. }
  1498. else if($val['order_status_id'] == 6 && $val['delivery'] =='pickup')
  1499. {
  1500. //delivery 6
  1501. $val['status_name'] = '已自提';
  1502. //已自提
  1503. }
  1504. else if($val['order_status_id'] == 1 && $val['type'] == 'lottery')
  1505. {
  1506. //等待开奖
  1507. //一等奖
  1508. if($val['lottery_win'] == 1)
  1509. {
  1510. $val['status_name'] = '一等奖';
  1511. }else {
  1512. $val['status_name'] = '等待开奖';
  1513. }
  1514. }
  1515. else if($val['order_status_id'] == 2 && $val['type'] == 'lottery')
  1516. {
  1517. //等待开奖
  1518. $val['status_name'] = '等待开奖';
  1519. }
  1520. $val['hash_order_id']= $hashids->encode($val['order_id']);
  1521. $quantity = 0;
  1522. $goods_sql = "select order_goods_id,head_disc,member_disc,level_name,goods_id,is_pin,shipping_fare,name,goods_images,quantity,price,total,rela_goodsoption_valueid
  1523. from ".C('DB_PREFIX')."order_goods where order_id= ".$val['order_id']."";
  1524. $goods_list = M()->query($goods_sql);
  1525. foreach($goods_list as $kk => $vv)
  1526. {
  1527. $order_option_list = M('order_option')->where( array('order_goods_id' =>$vv['order_goods_id']) )->select();
  1528. $vv['goods_images']= C('SITE_URL') .resize($vv['goods_images'], C('common_image_thumb_width'), C('common_image_thumb_height'));
  1529. //price orign_price
  1530. $goods_filed = M('goods')->field('price')->where( array('goods_id' => $vv['goods_id']) )->find();
  1531. $vv['orign_price'] = $goods_filed['price'];
  1532. $quantity += $vv['quantity'];
  1533. foreach($order_option_list as $option)
  1534. {
  1535. $vv['option_str'][] = $option['value'];
  1536. }
  1537. if( !isset($vv['option_str']) )
  1538. {
  1539. $vv['option_str'] = '';
  1540. }else{
  1541. $vv['option_str'] = implode(',', $vv['option_str']);
  1542. }
  1543. $vv['price'] = round($vv['price'],2);
  1544. $vv['orign_price'] = round($vv['orign_price'],2);
  1545. //price orign_price
  1546. $goods_list[$kk] = $vv;
  1547. }
  1548. $val['quantity'] = $quantity;
  1549. if( empty($val['store_id']) )
  1550. {
  1551. $val['store_id'] = 1;
  1552. }
  1553. $store_info = M('seller')->field('s_true_name,s_logo')->where('s_id='.$val['store_id'])->find();
  1554. $store_info['s_logo'] = C('SITE_URL').'/Uploads/image/'.$store_info['s_logo'];
  1555. $val['store_info'] = $store_info;
  1556. $val['goods_list'] = $goods_list;
  1557. if($val['type'] == 'integral')
  1558. {
  1559. //$order_id
  1560. $integral_order = M('integral_order')->field('score')->where( array('order_id' => $val['order_id']) )->find();
  1561. $val['score'] = intval($integral_order['score']);
  1562. }
  1563. $val['total'] = round($val['total'],2);
  1564. $list[$key] = $val;
  1565. }
  1566. $need_data = array('code' => 0);
  1567. if( !empty($list) )
  1568. {
  1569. $need_data['data'] = $list;
  1570. }else {
  1571. $need_data = array('code' => 1);
  1572. }
  1573. echo json_encode( $need_data );
  1574. die();
  1575. }
  1576. public function order_info()
  1577. {
  1578. $token = I('get.token');
  1579. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  1580. $member_id = $weprogram_token['member_id'];
  1581. $order_id = I('get.id',0);
  1582. $order_info = M('order')->where( array('order_id' => $order_id, 'member_id' => $member_id) )->find();
  1583. $pick_up_info = array();
  1584. $pick_order_info = array();
  1585. if( $order_info['delivery'] == 'pickup' )
  1586. {
  1587. //查询自提点
  1588. $pick_order_info = M('pick_order')->where( array('order_id' => $order_id) )->find();
  1589. $pick_id = $pick_order_info['pick_id'];
  1590. $pick_up_info = M('pick_up')->where( array('id' => $pick_id) )->find();
  1591. }
  1592. //$this->pick_up_info = $pick_up_info;
  1593. $order_status_info = M('order_status')->where( array('order_status_id' => $order_info['order_status_id']) )->find();
  1594. //10 name
  1595. if($order_info['order_status_id'] == 10)
  1596. {
  1597. $order_status_info['name'] = '等待退款';
  1598. }
  1599. else if($order_info['order_status_id'] == 4 && $order_info['delivery'] =='pickup')
  1600. {
  1601. //delivery 6
  1602. $order_status_info['name'] = '待自提';
  1603. //已自提
  1604. }
  1605. else if($order_info['order_status_id'] == 6 && $order_info['delivery'] =='pickup')
  1606. {
  1607. //delivery 6
  1608. $order_status_info['name'] = '已自提';
  1609. }
  1610. else if($order_info['order_status_id'] == 1 && $order_info['type'] == 'lottery')
  1611. {
  1612. //等待开奖
  1613. //一等奖
  1614. if($order_info['lottery_win'] == 1)
  1615. {
  1616. $order_status_info['name'] = '一等奖';
  1617. }else {
  1618. $order_status_info['name'] = '等待开奖';
  1619. }
  1620. }
  1621. $shipping_province = M('area')->where( array('area_id' => $order_info['shipping_province_id']) )->find();
  1622. $shipping_city = M('area')->where( array('area_id' => $order_info['shipping_city_id']) )->find();
  1623. $shipping_country = M('area')->where( array('area_id' => $order_info['shipping_country_id']) )->find();
  1624. $order_goods_list = M('order_goods')->where( array('order_id' => $order_id) )->select();
  1625. $shiji_total_money = 0;
  1626. $member_youhui = 0.00;
  1627. foreach($order_goods_list as $key => $order_goods)
  1628. {
  1629. $order_option_info = M('order_option')->field('value')->where( array('order_id' =>$order_id,'order_goods_id' => $order_goods['order_goods_id']) )->select();
  1630. foreach($order_option_info as $option)
  1631. {
  1632. $vv['option_str'][] = $option['value'];
  1633. }
  1634. if(empty($vv['option_str']))
  1635. {
  1636. //option_str
  1637. $order_goods['option_str'] = '';
  1638. }else{
  1639. $order_goods['option_str'] = implode(',', $vv['option_str']);
  1640. }
  1641. //
  1642. $order_goods['shipping_fare'] = round($order_goods['shipping_fare'],2);
  1643. $order_goods['price'] = round($order_goods['price'],2);
  1644. $order_goods['total'] = round($order_goods['total'],2);
  1645. $order_goods['real_total'] = round($order_goods['quantity'] * $order_goods['price'],2);
  1646. $order_goods['image']=C('SITE_URL').resize($order_goods['goods_images'], C('common_image_thumb_width'), C('common_image_thumb_height'));
  1647. $order_goods['goods_images']= C('SITE_URL').'/Uploads/image/'.$order_goods['goods_images'];
  1648. $goods_info = M('goods')->field('price')->where( array('goods_id' => $order_goods['goods_id']) )->find();
  1649. $order_goods['shop_price'] = $goods_info['price'];
  1650. $store_info = M('seller')->field('s_true_name,s_logo')->where('s_id='.$order_goods['store_id'])->find();
  1651. $store_info['s_logo'] = C('SITE_URL').'/Uploads/image/'.$store_info['s_logo'];
  1652. $order_goods['store_info'] = $store_info;
  1653. unset($order_goods['model']);
  1654. unset($order_goods['rela_goodsoption_valueid']);
  1655. unset($order_goods['comment']);
  1656. $order_goods_list[$key] = $order_goods;
  1657. $shiji_total_money += $order_goods['quantity'] * $order_goods['price'];
  1658. $member_youhui += ($order_goods['real_total'] - $order_goods['total']);
  1659. }
  1660. unset($order_info['store_id']);
  1661. //unset($order_info['type']);
  1662. unset($order_info['email']);
  1663. unset($order_info['shipping_city_id']);
  1664. unset($order_info['shipping_country_id']);
  1665. unset($order_info['shipping_province_id']);
  1666. unset($order_info['comment']);
  1667. unset($order_info['voucher_id']);
  1668. //unset($order_info['voucher_credit']);
  1669. unset($order_info['is_balance']);
  1670. unset($order_info['lottery_win']);
  1671. unset($order_info['ip']);
  1672. unset($order_info['ip_region']);
  1673. unset($order_info['user_agent']);
  1674. $order_info['shipping_fare'] = round($order_info['shipping_fare'],2) < 0.01 ? '0.00':round($order_info['shipping_fare'],2) ;
  1675. $order_info['total'] = round($order_info['total'],2)< 0.01 ? '0.00':round($order_info['total'],2) ;
  1676. $order_info['real_total'] = round($shiji_total_money,2)+$order_info['shipping_fare'];
  1677. $order_info['price'] = round($order_info['price'],2);
  1678. $order_info['member_youhui'] = round($member_youhui,2) < 0.01 ? '0.00':round($member_youhui,2);
  1679. $order_info['date_added'] = date('Y-m-d H:i:s', $order_info['date_added']);
  1680. $need_data = array();
  1681. //{{order.order_info.total + order.order_info.shipping_fare - order.order_info.voucher_credit}}
  1682. if($order_info['delivery'] =='pickup')
  1683. {
  1684. //$order_info['total'] = $order_info['total'] - $order_info['voucher_credit'];
  1685. }else{
  1686. //$order_info['total'] = $order_info['total'] + $order_info['shipping_fare'] - $order_info['voucher_credit'];
  1687. }
  1688. if($order_info['type'] == 'integral')
  1689. {
  1690. //$order_id
  1691. $integral_order = M('integral_order')->field('score')->where( array('order_id' => $order_id) )->find();
  1692. $need_data['score'] = intval($integral_order['score']);
  1693. }
  1694. $need_data['order_info'] = $order_info;
  1695. $need_data['order_status_info'] = $order_status_info;
  1696. $need_data['shipping_province'] = $shipping_province;
  1697. $need_data['shipping_city'] = $shipping_city;
  1698. $need_data['shipping_country'] = $shipping_country;
  1699. $need_data['order_goods_list'] = $order_goods_list;
  1700. //$order_info['order_status_id'] 13 平台介入退款
  1701. $order_refund_historylist = array();
  1702. $pingtai_deal = 0;
  1703. //判断是否已经平台处理完毕
  1704. $order_refund_historylist = M('order_refund_history')->where( array('order_id' => $order_id) )->order('addtime asc')->select();
  1705. foreach($order_refund_historylist as $key => $val)
  1706. {
  1707. if($val['type'] ==3)
  1708. {
  1709. $pingtai_deal = 1;
  1710. }
  1711. }
  1712. //order_refund
  1713. $order_refund = M('order_refund')->where( array('order_id' => $order_id) )->find();
  1714. if(!empty($order_refund))
  1715. {
  1716. $order_refund['addtime'] = date('Y-m-d H:i:s', $order_refund['addtime']);
  1717. }
  1718. $need_data['pick_up'] = $pick_up_info;
  1719. if( empty($pick_order_info['qrcode']) )
  1720. {
  1721. //qrcode
  1722. $jssdk = new \Lib\Weixin\Jssdk( $this->appid, $this->appsecret);
  1723. $weqrcode = $jssdk->getWeQrcode($pick_order_info['pick_sn']);
  1724. //保存图片
  1725. $image_dir = ROOT_PATH.'Uploads/image/goods';
  1726. $image_dir .= '/'.date('Y-m-d').'/';
  1727. $file_path = C('SITE_URL').'Uploads/image/goods/'.date('Y-m-d').'/';
  1728. $kufile_path = $dir.'/'.date('Y-m-d').'/';
  1729. RecursiveMkdir($image_dir);
  1730. $file_name = md5('qrcode_'.$pick_order_info['pick_sn'].time()).'.png';
  1731. //qrcode
  1732. file_put_contents($image_dir.$file_name, $weqrcode);
  1733. //pick_order_info
  1734. M('pick_order')->where( array('id' => $pick_order_info['id'] ) )->save( array('qrcode' => $file_path.$file_name) );
  1735. $pick_order_info['qrcode'] = $file_path.$file_name;
  1736. }
  1737. $need_data['pick_order_info'] = $pick_order_info;
  1738. //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
  1739. //https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
  1740. echo json_encode( array('code' => 0,'data' => $need_data,'pingtai_deal' => $pingtai_deal,'order_refund' => $order_refund ) );
  1741. die();
  1742. }
  1743. public function order_all_show()
  1744. {
  1745. $token = I('get.token');
  1746. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  1747. $member_id = $weprogram_token['member_id'];
  1748. $order_all_id = I('get.id',0);
  1749. $order_all_info = M('order_all')->where( array('id' => $order_all_id) )->find();
  1750. $order_relate_list = M('order_relate')->where( array('order_all_id' => $order_all_id) )->select();
  1751. $need_data = array();
  1752. foreach($order_relate_list as $relate_val)
  1753. {
  1754. $order_id = $relate_val['order_id'];
  1755. $order_info = M('order')->where( array('order_id' => $order_id, 'member_id' => $member_id) )->find();
  1756. $order_status_info = M('order_status')->where( array('order_status_id' => $order_info['order_status_id']) )->find();
  1757. $shipping_province = M('area')->where( array('area_id' => $order_info['shipping_province_id']) )->find();
  1758. $shipping_city = M('area')->where( array('area_id' => $order_info['shipping_city_id']) )->find();
  1759. $shipping_country = M('area')->where( array('area_id' => $order_info['shipping_country_id']) )->find();
  1760. $order_goods_list = M('order_goods')->where( array('order_id' => $order_id) )->select();
  1761. foreach($order_goods_list as $key => $order_goods)
  1762. {
  1763. $order_option_info = M('order_option')->field('value')->where( array('order_id' =>$order_id,'order_goods_id' => $order_goods['order_goods_id']) )->select();
  1764. foreach($order_option_info as $option)
  1765. {
  1766. $vv['option_str'][] = $option['value'];
  1767. }
  1768. $order_goods['option_str'] = implode(',', $vv['option_str']);
  1769. $order_goods['image']=C('SITE_URL').resize($order_goods['goods_images'], C('common_image_thumb_width'), C('common_image_thumb_height'));
  1770. $order_goods['goods_images']= C('SITE_URL').'/Uploads/image/'.$order_goods['goods_images'];
  1771. $store_info = M('seller')->field('s_true_name,s_logo')->where('s_id='.$order_goods['store_id'])->find();
  1772. $store_info['s_logo'] = C('SITE_URL').'/Uploads/image/'.$store_info['s_logo'];
  1773. $order_goods['store_info'] = $store_info;
  1774. unset($order_goods['model']);
  1775. unset($order_goods['rela_goodsoption_valueid']);
  1776. unset($order_goods['comment']);
  1777. $order_goods_list[$key] = $order_goods;
  1778. }
  1779. $shipping_address = $order_info['shipping_address'];
  1780. $shipping_name = $order_info['shipping_name'];
  1781. $telephone = $order_info['telephone'];
  1782. unset($order_info['store_id']);
  1783. unset($order_info['type']);
  1784. unset($order_info['email']);
  1785. unset($order_info['shipping_city_id']);
  1786. unset($order_info['shipping_country_id']);
  1787. unset($order_info['shipping_province_id']);
  1788. unset($order_info['comment']);
  1789. unset($order_info['voucher_id']);
  1790. unset($order_info['voucher_credit']);
  1791. unset($order_info['is_balance']);
  1792. unset($order_info['lottery_win']);
  1793. unset($order_info['ip']);
  1794. unset($order_info['ip_region']);
  1795. unset($order_info['user_agent']);
  1796. $tmp_arr = array();
  1797. $tmp_arr['order_goods_list'] = $order_goods_list;
  1798. $tmp_arr['order_info'] = $order_info;
  1799. $need_data[] = $tmp_arr;
  1800. }
  1801. //$this->order_status_info = $order_status_info;
  1802. $need_datas = array();
  1803. $need_datas['order_status_info'] = $order_status_info;
  1804. $need_datas['shipping_province'] = $shipping_province;
  1805. $need_datas['shipping_city'] = $shipping_city;
  1806. $need_datas['shipping_country'] = $shipping_country;
  1807. $need_datas['shipping_name'] = $shipping_name;
  1808. $need_datas['telephone'] = $telephone;
  1809. $need_datas['shipping_address'] = $shipping_address;
  1810. $need_datas['order_list'] = $need_data;
  1811. echo json_encode( array('code' => 0,'data' => $need_datas ) );
  1812. die();
  1813. }
  1814. public function applogin()
  1815. {
  1816. $code = I('get.code');
  1817. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->appid}&secret={$this->appsecret}&js_code={$code}&grant_type=authorization_code";
  1818. $open_str = $this->http_request($url);
  1819. //"unionid":"o0v630n0_IgASw5-k7RGFO-i8oeI"
  1820. $data = json_decode($open_str, true);
  1821. $expires_time = time() + $data['expires_in'];
  1822. $token = md5($data['openid'].time());
  1823. S('wepro_openid_'.$token, $data['openid']);
  1824. S('wepro_expires_time_'.$token, $expires_time);
  1825. S('wepro_session_key_'.$token, $data['session_key']);
  1826. S('wepro_unionid_'.$token, $data['unionid']);
  1827. $werp_data = array();
  1828. $werp_data['token'] = $token;
  1829. $result = array('code' => 1, 'token' => $token,'openid' =>$data['openid']);
  1830. echo json_encode($result);
  1831. die();
  1832. }
  1833. /**
  1834. 小程序授权登录
  1835. **/
  1836. public function applogin_do()
  1837. {
  1838. $token = I('get.token');
  1839. $data_json = file_get_contents('php://input');
  1840. $data = json_decode($data_json, true);
  1841. $user_info = $data['userinfo'];
  1842. $share_id = $data['share_id'];
  1843. $openid = S('wepro_openid_'.$token);
  1844. $expires_time = S('wepro_expires_time_'.$token);
  1845. $session_key = S('wepro_session_key_'.$token);
  1846. $unionid = S('wepro_unionid_'.$token);
  1847. $user_info['nickName'] = \Lib\Weixin\WeChatEmoji::clear($user_info['nickName']);
  1848. $user_info['nickName'] = trim($user_info['nickName']);
  1849. $member_info = M('member')->where( array('we_openid' =>$openid) )->find();
  1850. if( !empty($unionid) && empty($member_info) )
  1851. {
  1852. $member_info = M('member')->where( array('unionid' =>$unionid) )->find();
  1853. }
  1854. if(!empty($member_info) )
  1855. {
  1856. $data = array();
  1857. $data['member_id'] = $member_info['member_id'];
  1858. $data['we_openid'] = trim($openid);
  1859. $data['avatar'] = trim($user_info['avatarUrl']);
  1860. $data['last_login_time'] = time();
  1861. $data['login_count'] = array('exp','login_count+1');
  1862. $data['last_login_ip'] = get_client_ip();
  1863. M('Member')->save($data);
  1864. $member_id = $member_info['member_id'];
  1865. $weprogram_token_data = array();
  1866. $weprogram_token_data['token'] = $token;
  1867. $weprogram_token_data['member_id'] = $member_id;
  1868. $weprogram_token_data['session_key'] = $session_key;
  1869. $weprogram_token_data['expires_in'] = $expires_time;
  1870. M('weprogram_token')->add($weprogram_token_data);
  1871. }else {
  1872. $data = array();
  1873. $data['email']= time().mt_rand(1,9999).'@lf.com';
  1874. $data['uname']=trim($user_info['nickName']);
  1875. $data['name']=trim($user_info['nickName']);
  1876. $data['avatar']=trim($user_info['avatarUrl']);
  1877. $data['openid'] = $openid;
  1878. $data['we_openid'] = trim($openid);
  1879. $data['unionid'] = trim($unionid);
  1880. //share_id
  1881. $data['share_id'] = $share_id;
  1882. $data['reg_type'] = 'weprogram';
  1883. $data['pwd'] = think_ucenter_encrypt($user_info['nickName'],C('PWD_KEY'));
  1884. $data['status']=1;
  1885. $data['create_time'] = time();
  1886. $data['last_login_ip'] = get_client_ip();
  1887. $member_id= M('Member')->add($data);
  1888. $weprogram_token_data = array();
  1889. $weprogram_token_data['token'] = $token;
  1890. $weprogram_token_data['member_id'] = $member_id;
  1891. $weprogram_token_data['session_key'] = $session_key;
  1892. $weprogram_token_data['expires_in'] = $expires_time;
  1893. M('weprogram_token')->add($weprogram_token_data);
  1894. if($share_id > 0)
  1895. {
  1896. $share_member = M('member')->field('we_openid')->where( array('member' => $share_id) )->find();
  1897. $member_formid_info = M('member_formid')->where( array('member_id' => $share_id, 'state' => 0) )->find();
  1898. //更新
  1899. if(!empty($member_formid_info))
  1900. {
  1901. $template_data['keyword1'] = array('value' => $data['name'], 'color' => '#030303');
  1902. $template_data['keyword2'] = array('value' => '普通会员', 'color' => '#030303');
  1903. $template_data['keyword3'] = array('value' => date('Y-m-d H:i:s'), 'color' => '#030303');
  1904. $template_data['keyword4'] = array('value' => '恭喜你,获得一位新成员', 'color' => '#030303');
  1905. $pay_order_msg_info = M('config')->where( array('name' => 'wxprog_member_take_in') )->find();
  1906. $template_id = $pay_order_msg_info['value'];
  1907. $url =C('SITE_URL');
  1908. $pagepath = 'pages/dan/me';
  1909. send_wxtemplate_msg($template_data,$url,$pagepath,$share_member['we_openid'],$template_id,$member_formid_info['formid']);
  1910. M('member_formid')->where( array('id' => $member_formid_info['id']) )->save( array('state' => 1) );
  1911. }
  1912. }
  1913. }
  1914. echo json_encode(array('code' =>1,'member_id' => $member_id));
  1915. die();
  1916. }
  1917. /**
  1918. 获取用户信息
  1919. **/
  1920. public function me()
  1921. {
  1922. $token = I('get.token');
  1923. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  1924. if(empty($weprogram_token))
  1925. {
  1926. $data = array('code' =>1);
  1927. } else{
  1928. $member_info = M('member')->field('name,avatar')->where( array('member_id' => $weprogram_token['member_id']) )->find();
  1929. $user_info = array();
  1930. $user_info['headimgurl'] = $member_info['avatar'];
  1931. $user_info['nickname'] = $member_info['name'];
  1932. $data = array('code' =>0, 'user_info' => $user_info);
  1933. }
  1934. echo json_encode($data);
  1935. die();
  1936. }
  1937. private function xml($xml){
  1938. $p = xml_parser_create();
  1939. xml_parse_into_struct($p, $xml, $vals, $index);
  1940. xml_parser_free($p);
  1941. $data = "";
  1942. foreach ($index as $key=>$value) {
  1943. if($key == 'xml' || $key == 'XML') continue;
  1944. $tag = $vals[$value[0]]['tag'];
  1945. $value = $vals[$value[0]]['value'];
  1946. $data[$tag] = $value;
  1947. }
  1948. return $data;
  1949. }
  1950. function http_request($url,$data = null,$headers=array())
  1951. {
  1952. $curl = curl_init();
  1953. if( count($headers) >= 1 ){
  1954. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  1955. }
  1956. curl_setopt($curl, CURLOPT_URL, $url);
  1957. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  1958. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  1959. if (!empty($data)){
  1960. curl_setopt($curl, CURLOPT_POST, 1);
  1961. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  1962. }
  1963. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  1964. $output = curl_exec($curl);
  1965. curl_close($curl);
  1966. return $output;
  1967. }
  1968. private function sign($data){
  1969. $stringA = '';
  1970. foreach ($data as $key=>$value){
  1971. if(!$value) continue;
  1972. if($stringA) $stringA .= '&'.$key."=".$value;
  1973. else $stringA = $key."=".$value;
  1974. }
  1975. $wx_key = $this->pay_key;
  1976. $stringSignTemp = $stringA.'&key='.$wx_key;
  1977. return strtoupper(md5($stringSignTemp));
  1978. }
  1979. private function nonce_str(){
  1980. $result = '';
  1981. $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
  1982. for ($i=0;$i<32;$i++){
  1983. $result .= $str[rand(0,48)];
  1984. }
  1985. return $result;
  1986. }
  1987. }